繁体   English   中英

有条件地对应用程序的一部分进行身份验证,例如,在Windows身份验证上对应用程序/ secure进行身份验证,在OAuth2上对应用程序的其余部分进行身份验证

[英]conditionally authenticate one part e.g /secure of the application on Windows Authentication and rest of the application on OAuth2

我使用HTTPListener和OWIN将asp.net webapi作为Windows服务托管。我想有条件地对一个部分进行身份验证,例如Windows身份验证上的应用程序的/ secure和OAuth2上其余应用程序的身份。这在IIS中很容易,但事实证明使用OWIN + HttpListenr并不是那么简单。

我按照本文设置了Windows身份验证,但它适用于整个应用程序

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

                app.Run(context =>
                {
                    context.Response.ContentType = "text/plain";
                    return context.Response.WriteAsync("Hello World!");
                });
            }
        }
    }

您可以使用HttpListener.AuthenticationSchemeSelectorDelegate添加一些条件逻辑。

namespace KatanaSelfHost
    {
        class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                HttpListener listener = 
                    (HttpListener)app.Properties["System.Net.HttpListener"];                    
listener.AuthenticationSchemeSelectorDelegate = 
    new AuthenticationSchemeSelector (AuthenticationSchemeForClient);


                app.Run(context =>
                {
                    context.Response.ContentType = "text/plain";
                    return context.Response.WriteAsync("Hello World!");
                });
            }
        }
    }
    static AuthenticationSchemes AuthenticationSchemeForClient(HttpListenerRequest request)
    {
        Console.WriteLine("Client authentication protocol selection in progress...");
        // check the url here
        if (request.RawUrl.Contains("/secure"))
        {
            return AuthenticationSchemes.IntegratedWindowsAuthentication;
        }
        else
        {
            return AuthenticationSchemes.None;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM