1

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

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);

    }
}
4

1 回答 1

2

这在这里清楚地解释了 - https://msdn.microsoft.com/en-us/library/aa302377.aspx

ASP.NET 提供以下主体和标识对象实现:

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

下表说明了对于一系列 IIS 身份验证设置,从维护IPrincipal和/或 IIdentity对象的每个变量中获得的结果标识。表中使用了以下缩写:

  • HttpContext = HttpContext.Current.User,它返回一个 IPrincipal对象,其中包含当前 Web 请求的安全信息。这是经过身份验证的 Web 客户端。
  • WindowsIdentity = WindowsIdentity.GetCurrent(),它返回当前正在执行的 Win32 线程的安全上下文的标识。
  • Thread = Thread.CurrentPrincipal,它返回当前执行的 .NET 线程的主体,该线程位于 Win32 线程之上。

注意   对于在 Windows Server 2003 上运行的 IIS 6.0,身份矩阵可以工作,只是 Machine\ASPNET 身份被替换为 NT Authority\Network Service。

在此处输入图像描述

于 2017-03-06T05:10:01.397 回答