How do I share my asp.net identity with an ajax enabled web service
I have an Asp.net application with Forms mode authentication
<authentication mode="Forms">
<forms loginUrl="Login.aspx" />
</authentication>
I have also created an Ajax enabled web service to support a 'vote' function from the page..
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="VoteServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="t开发者_运维问答rue"
multipleSiteBindingsEnabled="true" />
<services>
<service name="VoteService">
<endpoint address="" behaviorConfiguration="VoteServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="VoteService" />
</service>
</services>
in the service implementation, I need to get at the asp identity in order to figure out who is voting...
[OperationContract]
public int VoteForComment(Guid commentId)
{
string UserName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
//...
}
But even after logging in to the asp application , the ServiceSecurityContext.Current is null.
Is it possible to configure the web service so that it can share the asp identity? Alternately, is it possible to figure out the current user another way?
Edit Turns out really easy, the aspNetCompatabilityEnabled="true" means that the web service has access to all the normal state available in Asp.Net, I just had to use the System.Web namespace.... System.Web.HttpContext.Current.User.Identity.Name
My best bet would be use impersonation in the web.config.
<identity impersonate="true"/>
<authentication mode="Forms" />
this will give you Name provided by user in httpContext and in current thread.
WindowsPrincipal winPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal();
精彩评论