我有一个 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);
}
}