如何配置Owin以使用自定义AuthenticationHandler?

时间:2017-10-12 11:55:12

标签: c# owin openid-connect

have read可以创建自定义Owin身份验证处理程序,但我无法弄清楚如何配置Owin以使用我的处理程序而不是默认处理程序。

如何告诉Owin使用此类而不是默认类?

or

1 个答案:

答案 0 :(得分:1)

您必须将其添加为自定义AuthenticationMiddleware的一部分。

public class CustomAuthMiddleware : AuthenticationMiddleware<OpenIdConnectAuthenticationOptions>
{
    public CustomAuthMiddleware(OwinMiddleware nextMiddleware, OpenIdConnectAuthenticationOptions authOptions)
        : base(nextMiddleware, authOptions)
    { }

    protected override AuthenticationHandler<OpenIdConnectAuthenticationOptions> CreateHandler()
    {
        return new XDOpenIdAuthHandler(yourLogger);
    }
}

然后在Startup.Auth中使用它,例如:

public partial class Startup
{
    // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        app.Use<CustomAuthMiddleware>(new OpenIdConnectAuthenticationOptions());
    }
}

请注意,Owin管道不得包含默认的OpenIdConnectAuthenticationMiddleware,否则它仍将作为请求管道的一部分进行调用。

相关问题