WCF REST authentication check

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

The problem I am facing is the connection between the interceptor and the service. Nothing I 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 installed, I can’t access it in the behavior of the service 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:

  • How can I transfer data between Interceptor and a service?

  • Is there a canonical way to pass authentic information between them (note that auth information is the UID in the database, not the Windows identifier)?

thanks

+5
source share
1 answer

Do you create a SecureWebServiceHostFactory using 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 used this example, and it works, we’ll take a closer look at your code, you may miss something.

+2
source

Source: https://habr.com/ru/post/970985/


All Articles