首页 文章

ASP.Net MVC使用Application Gateway为授权重定向设置自定义主机名

提问于
浏览
0

场景:Azure应用程序网关背后的ASP.net MVC应用程序

当未经授权的用户尝试访问使用Authorize属性保护控制器方法的页面时,用户将被重定向到Azure AD登录页面 . 登录后,用户将被重定向到无法访问的应用程序URL(提供403),而不是正确的应用程序网关URL .

我们使用时登录正常工作:

HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);

所以问题是:是否有一种好方法可以覆盖Authorize属性正在使用的主机名,以便重定向到应用程序设置中指定的应用程序网关URL?

在startup.auth.cs内部,我们使用OpenId Connect并将重定向URI设置为正确的应用程序网关URL,该URL用于成功进行身份验证,但在用户通过身份验证后仍会重定向到错误的URL .

Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    RedirectToIdentityProvider = (context) =>
                    {
                        string appBaseUrl = _siteUrl;
                        context.ProtocolMessage.RedirectUri = appBaseUrl;

1 回答

  • 0

    从以下问题找到我自己的答案:Redirect user after authentication with OpenIdConnect in ASP.Net MVC

    总而言之,可以通过在AuthorizationCodeReceived回调中设置context.AuthenticationTicket.Properties.RedirectUri来实现身份验证后的重定向 .

    app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
    
    
                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        RedirectToIdentityProvider = (context) =>
                        {
    
                            _redirectUri = _siteUrl + context.Request.Path;
    
    
                            return Task.FromResult(0);
                        },
                        // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                        AuthorizationCodeReceived = async (context) =>
                        {
                            var httpContext =
                                HttpContext.Current.GetOwinContext().Environment["System.Web.HttpContextBase"] as
                                    HttpContextBase;
                            var code = context.Code;
                            context.AuthenticationTicket.Properties.RedirectUri = _redirectUri;
    
    
                           }
                        },
                        AuthenticationFailed = OnAuthenticationFailed
                    }
                });
    

相关问题