Azure AD Open ID Connect OAuth 2.0在ASP.NET Web APP和Web API中的无限重定向循环

8 浏览
0 Comments

Azure AD Open ID Connect OAuth 2.0在ASP.NET Web APP和Web API中的无限重定向循环

使用ASP.NET web应用程序可以从任何Azure Active Directory(Azure AD)实例登录个人帐户和工作和学校帐户。

OWIN中间件NuGet软件包

Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb

OWIN启动类

OWIN中间件使用启动类在托管进程初始化时运行。在本快速入门中,启动.cs文件位于根文件夹中。以下代码显示了本快速入门所使用的参数。

public void Configuration(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            // 从web.config中获取ClientId、authority和RedirectUri
            ClientId = clientId,
            Authority = authority,
            RedirectUri = redirectUri,
            // PostLogoutRedirectUri是用户在退出登录后将被重定向到的页面。在这种情况下,它使用主页
            PostLogoutRedirectUri = redirectUri,
            Scope = OpenIdConnectScope.OpenIdProfile,
            // ResponseType设置为请求id_token,其中包含有关已登录用户的基本信息
            ResponseType = OpenIdConnectResponseType.IdToken,
            // 将ValidateIssuer设置为false,允许来自任何组织的个人和工作帐户登录到您的应用程序
            // 要仅允许来自单个组织的用户,请将ValidateIssuer设置为true,并在web.config中的'tenant'设置中设置为租户名称
            // 要仅允许来自一系列特定组织的用户,请将ValidateIssuer设置为true,并使用ValidIssuers参数
            TokenValidationParameters = new TokenValidationParameters()
            {
                ValidateIssuer = false // 简化(请参阅下面的注释)
            },
            // OpenIdConnectAuthenticationNotifications配置OWIN将身份验证失败的通知发送到OnAuthenticationFailed方法
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = OnAuthenticationFailed
            }
        }
    );
}

ASP.NET MVC / Web API

//您可以通过在控制器中请求身份验证挑战来强制用户登录:
public void SignIn()
{
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties{ RedirectUri = "/" },
            OpenIdConnectAuthenticationDefaults.AuthenticationType);
    }
}

ASP.NET Web表单:

 protected void Login_click(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                HttpContext.Current.GetOwinContext().Authentication.Challenge(
                    new AuthenticationProperties { RedirectUri = "/" },
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }
        }

0
0 Comments

问题:Azure AD Open ID Connect OAuth 2.0 在ASP.NET Web应用程序和Web API中出现无限重定向循环的问题。

解决方法:

此问题已在ASP.NET Core中得到解决,并且在ASP.NET的新版本的Katana Owin中也得到了解决。为了解决这个问题,您可以将应用程序升级到使用ASP.NET Core。如果必须继续使用ASP.NET,执行以下操作:

1. 更新应用程序的Microsoft.Owin.Host.SystemWeb包至少为版本3.1.0.0。

2. 修改代码以使用其中一个新的cookie管理器类,例如以下示例代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = "Cookies", 
    CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager() 
});

0