I have a OWIN hosted web api which runs as Network Service with WindowsAuthentication enabled by the following line in Configuration method of OWIN Startup class.
HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
Everything works fine except when i try getting user details, by
caller = System.Security.Principal.WindowsIdentity.GetCurrent();
Returns:AuthenticationType: "Negotiate", Name: "NT AUTHORITY\NETWORK SERVICE"ApiController.User.Identity
Returns:AuthenticationType: "NTLM", Name: "Domain\Username"
I actually expected the credentials which ApiController.User.Identity gave. I'm confused about why i got Different results in both. Can anyone help me with this?
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 startup class:
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);
}
}
This is clearly explained here - https://msdn.microsoft.com/en-us/library/aa302377.aspx