WCF REST RequestInterceptor authentication

丶灬走出姿态 提交于 2019-12-20 14:43:05

问题


I am trying to do some basic authentication in a WCF RequestInterceptor. I am using this article as a start.

The problem I am running into is communicating between the interceptor and the service. Nothing I have tried seems to work. So far, I have tried:

  • OperationContext.Current
  • requestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name]["foo"] = value
  • HttpContext.Current.Request

But no matter what I set, I can't seem to access it in the service behavior itself:

[AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
public class AdvertiserService : ApiServiceBase<AdvertiserDataAccessor>, IAdvertiserService
{
    [WebGet( UriTemplate = "" )]
    public List<Advertiser> GetAdvertisers()
    {
        var request = HttpContext.Current.Request;
        var headers = HttpContext.Current.Request.Headers;
        var p = HttpContext.Current.Request.Headers["Principal"];

        OperationContext ctx = OperationContext.Current;
     }
}

My questions are:

  1. How can I pass data between the Interceptor and the service?

  2. Is there a canoncial way to pass auth information between them (note, the auth info is a UID in the database, not a Windows Identity)?

Thanks


回答1:


Are you creating the SecureWebServiceHostFactory with your Interceptor?

public class SecureWebServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
         WebServiceHost2 host = new WebServiceHost2(serviceType, true, baseAddresses);
         host.Interceptors.Add(new AuthenticationInterceptor());
         return host;
     }
}

I have used that example and it works, take a closer look to your code, you might be missing something.



来源:https://stackoverflow.com/questions/3445296/wcf-rest-requestinterceptor-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!