How to share your asp.net id using ajax-enabled web service

I have an Asp.net application with forms mode authentication

<authentication mode="Forms"> <forms loginUrl="Login.aspx" /> </authentication> 

I also created an Ajax-enabled web service to support the voting feature from the page.

  <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="VoteServiceAspNetAjaxBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="VoteService"> <endpoint address="" behaviorConfiguration="VoteServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="VoteService" /> </service> </services> 

in the service implementation, I need to get the asp id to find out who is voting ...

 [OperationContract] public int VoteForComment(Guid commentId) { string UserName = ServiceSecurityContext.Current.PrimaryIdentity.Name; //... } 

But even after entering the asp application, ServiceSecurityContext.Current is null.

Is it possible to configure a web service so that it can share the asp id? Alternatively, is it possible to find out the current user in a different way?


Change It's very simple, aspNetCompatabilityEnabled = "true" means that the web service has access to all the normal states available in Asp.Net, I just needed to use the System.Web .... System.Web.HttpContext.Current namespace. User.Identity.Name

+4
source share
1 answer

Best would be to use impersonation in web.config.

 <identity impersonate="true"/> <authentication mode="Forms" /> 

this will give you the name provided by the user in the httpContext and in the current stream.

WindowsPrincipal winPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal ();

hope this helps

Routes

+2
source

All Articles