WebApi with OWIN SelfHost and Windows Authentication

后端 未结 1 1156
迷失自我
迷失自我 2020-12-10 06:54

I have a console application SERVER that hosts WebApi controllers using OWIN self-hosting, and runs under a custom account named \"ServiceTest1\".

In the same machin

相关标签:
1条回答
  • 2020-12-10 07:19

    Your question is a little unclear on exactly how you've implemented the Windows authentication.

    Enable Windows authentication:

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
            listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
    
            // ...
        }
    }
    

    Get the user in an OWIN middleware:

    public async Task Invoke(IDictionary<string, object> env)
    {
        OwinContext context = new OwinContext(env);
        WindowsPrincipal user = context.Request.User as WindowsPrincipal;
    
        //...
    }
    

    Get the user in a Web API Controller:

    // In a web api controller function
    WindowsPrincipal user = RequestContext.Principal as WindowsPrincipal;
    
    0 讨论(0)
提交回复
热议问题