【问题标题】:ASP.Net MVC Set custom hostname for authorization redirect with Application GatewayASP.Net MVC 使用应用程序网关为授权重定向设置自定义主机名
【发布时间】:2018-10-03 16:14:27
【问题描述】:

场景: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;

【问题讨论】:

    标签: c# asp.net asp.net-mvc azure-application-gateway


    【解决方案1】:

    从以下问题中找到了我自己的答案: 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
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2013-12-14
      • 1970-01-01
      • 2013-10-31
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多