我会做一个小项目(WCF + REST),我有一个小问题。我希望让我的授权和身份验证类。
I'd make a little project (WCF + REST) and I have a small problem. I want make my Authorization and Authentication class.
我的授权类:
//validate api key public class BasicAuthorization : ServiceAuthorizationManager { public override bool CheckAccess(OperationContext operationContext, ref Message message) { //some code } }我的身份验证类
// validation user login & password public class BasicAuthentication : ServiceAuthenticationManager { public override ReadOnlyCollection<IAuthorizationPolicy> Authenticate( ReadOnlyCollection<IAuthorizationPolicy> authPolicy, Uri listenUri, ref Message message) { //some code } }我有太多一些配置文件
<behavior> <serviceAuthorization serviceAuthorizationManagerType="WCF.BasicAuthorization, WCF"/> <serviceAuthenticationManager serviceAuthenticationManagerType="WCF.BasicAuthentication, WCF"/> </behavior>在类的代码是不重要的 - 是没有问题的。
The code in class is unimportant - is not a problem.
我的问题是如何从的OperationContext或消息类获得头。以前我怎么说,我有这样的休息,所以我想手动设置Authorizaion头/ WWW-Authenticate头,但应用程序不会看到它。
My problem is how to get Headers from operationContext or message class. How i say before, i make this in rest, so i want manual set Authorizaion header / www-authenticate header, but application doesn't see it.
我打开在Fiddler2,并尝试提出的任何标题,例如:
I turn on the Fiddler2, and try put any header for example :
Content-Type: application/xml Authorization: Basic bla23rwerfsd3== User-Agent: Fiddler Host: localhost:59305和在message.Headers / operationContext.Headers不会有任何我的头(只等一个),没有授权,任何内容类型
And the message.Headers / operationContext.Headers doesn't has any my header (has only other one), no Authorization, no Content-Type
推荐答案您可以使用System.ServiceModel.Web.WebOperationContext类,它有一个静态属性当前,它代表了当前上下文你的网络运行期间访问头。它提供了包含类型WebHeaderCollection一个头属性的IncomingRequest属性。
You can access the headers during your web-operation using the System.ServiceModel.Web.WebOperationContext class, which has a static property "Current", which represents the current-context. It provides an "IncomingRequest" property that contains a "Header" property of type "WebHeaderCollection".
更多推荐
在页眉的OperationContext
发布评论