我有一个具有表单模式身份验证的 Asp.net 应用程序
<authentication mode="Forms">
<forms loginUrl="Login.aspx" />
</authentication>
我还创建了一个支持 Ajax 的 Web 服务来支持页面的“投票”功能。
<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>
在服务实现中,我需要获取 asp 身份才能确定谁在投票...
[OperationContract]
public int VoteForComment(Guid commentId)
{
string UserName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
//...
}
但即使登录到 asp 应用程序后,ServiceSecurityContext.Current 仍为 null。
是否可以配置 Web 服务,使其可以共享 asp 标识? 或者,是否可以通过其他方式找出当前用户?
<小时/>编辑结果非常简单,aspNetCompatabilityEnabled="true"意味着 Web 服务可以访问 Asp.Net 中可用的所有正常状态,我只需使用 System.Web 命名空间.... System.Web.HttpContext.Current.User.Identity.Name
最佳答案
我最好的选择是在 web.config 中使用模拟。
<identity impersonate="true"/>
<authentication mode="Forms" />
这将为您提供用户在 httpContext 和当前线程中提供的名称。
WindowsPrincipal winPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal();
关于asp.net - 如何与支持 ajax 的 Web 服务共享我的 asp.net 身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3915427/