【问题标题】:ApiController.User.Identity and System.Security.Principal.WindowsIdentity gives different user detailsApiController.User.Identity 和 System.Security.Principal.WindowsIdentity 提供不同的用户详细信息
【发布时间】:2017-03-06 04:39:03
【问题描述】:

我有一个 OWIN 托管的 web api,它以 Network Service 运行,WindowsAuthentication 通过 OWIN Startup 类的 Configuration 方法中的以下行启用。

HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

一切正常,除非我尝试获取用户详细信息,通过

  • caller = System.Security.Principal.WindowsIdentity.GetCurrent();
    返回:AuthenticationType: "Negotiate", Name: "NT AUTHORITY\NETWORK SERVICE"
  • ApiController.User.Identity
    返回:AuthenticationType: "NTLM", Name: "Domain\Username"

我实际上期待ApiController.User.Identity 提供的凭据。我很困惑为什么我在两者中都得到了不同的结果。谁能帮我这个?

public class CustomFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var caller = OperationContext.Current; //null
        caller = System.Web.HttpContext.Current; //null
        caller = actionContext.RequestContext.Principal.Identity as WindowsIdentity; //desired
        caller = System.Security.Principal.WindowsIdentity.GetCurrent(); //gives account details under which the project is hosted. 
    }
}

OWIN 启动类:

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
         HttpConfiguration config = new HttpConfiguration();
         HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
         listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

         config.MapHttpAttributeRoutes();
         config.MapODataServiceRoute(
                routeName: "ODataRoute",
                routePrefix: "Data",
                model: GetModel()
         );
         config.EnsureInitialized();
         appBuilder.UseWebApi(config);

    }
}

【问题讨论】:

  • 在 IIS 中运行 Web API 项目的用户帐户与登录到您的应用程序并调用 API 的用户不同。这就是为什么你在那里得到不同的价值。如果您想了解调用 Web API 的用户的详细信息,那么您应该使用 ApiController.User.Identity
  • 谢谢。那么如果我想在 ActionFilter 中获取这些详细信息呢?
  • 在 Action 过滤器中你应该可以访问 controllerContext 或者你可以简单地做Request.User.Identity
  • var caller = actionContext.RequestContext.Principal.Identity as WindowsIdentity;这工作正常。

标签: c# asp.net-web-api2 windows-authentication ntlm network-service


【解决方案1】:

这里解释得很清楚 - https://msdn.microsoft.com/en-us/library/aa302377.aspx

ASP.NET 提供以下主体和身份对象 实现:

  • WindowsPrincipalWindowsIdentity 对象表示已访问过的用户 通过 Windows 身份验证进行身份验证。有了这些对象, 角色列表自动从一组 Windows 组中获取到 Windows 用户所属的用户。
  • GenericPrincipalGenericIdentity 对象代表已被 使用表单身份验证或其他自定义身份验证 身份验证机制。有了这些对象,角色列表是 以自定义方式获取,通常从数据库中获取。
  • FormsIdentityPassportIdentity 对象代表拥有 已通过 Forms 和 Passport 身份验证进行身份验证 分别。

下表说明了针对 IIS 身份验证的范围 设置,从每个获得的结果身份 维持 IPrincipal 和/或 IIdentity 对象。以下缩写为 用于表中:

  • HttpContext = HttpContext.Current.User,返回一个 包含安全信息的 IPrincipal 对象 对于当前的 Web 请求。这是经过身份验证的 Web 客户。
  • WindowsIdentity = WindowsIdentity.GetCurrent(),返回 当前执行的 Win32 的安全上下文的标识 线程。
  • 线程 = Thread.CurrentPrincipal 返回主体 当前正在执行的 .NET 线程,它位于 Win32 之上 线程。
注意   在 Windows 上运行 IIS 6.0 Server 2003,身份矩阵工作,除了 Machine\ASPNET 身份被 NT Authority\Network Service 替换。

【讨论】:

  • HttpContext.Current 为空。 WindowsIdentity.GetCurrent() 提供服务帐户详细信息。 Thread.CurrentPrincipal 正在提供所需的详细信息。选择线程以获取详细信息是一种好习惯吗?
  • @AjayAradhya - 你确定HttpContext.Current 为空吗?可能你对 Controller 的 HttpContext 属性和 System.Web.HttpContext.Current 感到困惑; Controller.HttpContext - 控制器上下文在构造函数中将不可用(null)。如果我没记错的话,你可以尝试从System.Web.HttpContext.Current.User.Identity.Name获取身份名称
  • 我将 System.Web.HttpContext.Current 设为 null。
  • 检查一下 - brockallen.com/2013/10/27/… - Web API 2 引入了一个包含 Principal 属性的新 RequestContext 类。现在这是查找调用者身份的正确位置。这取代了 Thread.CurrentPrincipal 和/或 HttpContext.User 的先前机制。如果您正在编写代码以在 Web API 中对调用者进行身份验证,这也是您要分配的内容。
  • 当你自己托管时,你不依赖于 System.Web,或者换句话说,System.Web 解耦是 Web API 与 asp.net MVC 的区别,也是 Web Api 可以自我托管的原因:)
猜你喜欢
  • 2018-06-22
  • 2016-12-07
  • 2011-11-02
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 2019-12-31
相关资源
最近更新 更多