Azure AD身份验证重定向到主页

时间:2018-11-21 15:55:13

标签: azure azure-active-directory azure-authentication

我正在构建一个运行良好的Azure AD身份验证,唯一的问题是,如果用户单击未经过身份验证的类似于host.com/xxx/bbb的链接,则它们将重定向到root。

我仍然需要将它们重定向到他们在浏览器中输入的原始URL。能做到吗?以下是我在应用启动时使用的代码段:

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = XXX,
                Authority = string.Format("https://login.microsoftonline.com/{0}", YYY,

                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    RedirectToIdentityProvider = async n => { await Task.Run(() => SetRedirectUrl(n)); }
                }
            });
    }

private static void SetRedirectUrl(RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
    {
        notification.ProtocolMessage.RedirectUri = notification.Request.Uri.AbsoluteUri;
    }

OwinRequest的所有属性均未包含我要查找的完整路径。我也尝试过查看

HttpContext.Current.Request.Url

但这还没有完整的地址。

1 个答案:

答案 0 :(得分:0)

  

我仍然需要将它们重定向到他们输入的原始URL   浏览器

您需要在AccountController.cs中设置SignIn()方法。您可以使用“ Request.UrlReferrer.ToString();”获取输入的网址:

public void SignIn()
        {

            // var host = Request.UserHostName.ToString();
            var ori = Request.UrlReferrer.ToString();
            var index = ori.LastIndexOf('/');
            var action = ori.Substring(index);
            // Send an OpenID Connect sign-in request.
            if (!Request.IsAuthenticated)
            {
                HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = action },
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
            }
        }

相关问题