同时使用app.UseOpenIdConnectAuthentication时不使用CookieAuthenticationOptions.LoginPath值

ama*_*591 2 asp.net-mvc owin azure-active-directory openid-connect

我正在使用OWIN中间件进行Cookie身份验证和openIdConnect。在向cookie身份验证选项的启动身份验证代码中添加openIdConnect身份验证之前,LoginPath被用作重定向未经身份验证的用户的目标。这确实很好,并且是我想保留的功能。

但是,当我将app.UseOpenIdConnectAuthentication添加到我的项目时,它开始自动将未经身份验证的用户重定向到我的OpenIdConnect授权(https://login.windows.net/)。

有没有一种方法可以禁用OpenIdConnectAuthentication设置未经身份验证的用户的重定向路径,并依靠LoginPath设置Cookie身份验证?我目前的解决方法是在我的authorize属性中手动设置重定向路径,但是我想让OWIN中间件在可能的情况下进行处理。

谢谢。

码:

public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        var cookieOptions = new CookieAuthenticationOptions();
        cookieOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie;
        cookieOptions.LoginPath = new PathString("/Account/Login");

        app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);

        app.UseCookieAuthentication(cookieOptions);

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = FranchiseAuthType,
            ClientId = franchiseClientId,
            Authority = FranchiseAuthority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
        });
}
Run Code Online (Sandbox Code Playgroud)

小智 6

我不确定您是否能够解决此问题,但是您想要做的是添加

AuthenticationMode = AuthenticationMode.Passive
Run Code Online (Sandbox Code Playgroud)

到您的身份验证选项。这将使OpenIdConnect身份验证仅依赖于您的代码来进行调用。我相信这就是您打算发生的事情。

因此,您的新代码应如下所示:

public void ConfigureAuth(IAppBuilder app)
{
    // Enable the application to use a cookie to store information for the signed in user
    // and to use a cookie to temporarily store information about a user logging in with a third party login provider
    var cookieOptions = new CookieAuthenticationOptions();
    cookieOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie;
    cookieOptions.LoginPath = new PathString("/Account/Login");

    app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);

    app.UseCookieAuthentication(cookieOptions);

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {
        AuthenticationType = FranchiseAuthType,
        AuthenticationMode = AuthenticationMode.Passive,
        ClientId = franchiseClientId,
        Authority = FranchiseAuthority,
        PostLogoutRedirectUri = postLogoutRedirectUri,
    });
 }
Run Code Online (Sandbox Code Playgroud)