asp.net - 如何与支持 ajax 的 Web 服务共享我的 asp.net 身份

标签 asp.net ajax wcf

我有一个具有表单模式身份验证的 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/

相关文章:

c# - FormsAuthentication.RedirectToLoginPage() 不工作

asp.net - 阻止访问者打开某些页面

php - 使用 php/mysql 实时搜索防止多个同时查询

javascript - 更新内联不适用于 + 等符号

c# - WCF运行时异常 "Could not find default endpoint element that references..."

c# - 如何管理 log4net 以登录到特定的 Url 或 Web 服务

javascript - 每次提交页面时显示加载器 MVC Asp.net

javascript - DataTables - 返回 ajax 数据的格式

c# - WCF 客户端绑定(bind)以访问 https 服务。无法使用权​​限为 SSL/TLS 建立安全通道

具有 OAuth 身份验证的 WCF 服务