【问题标题】:How to get/set the userIdentity when AuthenticationMode is Passive in Azure Active directoryAzure Active Directory 中 AuthenticationMode 为 Passive 时如何获取/设置 userIdentity
【发布时间】:2019-04-26 23:09:25
【问题描述】:

我已将 AuthenticationMode 设置为被动并使用显式质询重定向到 azure 登录页面。

这很好用,但我需要一种编程方式来确定用户是否经过身份验证。我也想使用用户名,但在 HttpContext.User.Identity.IsAuthenticated 中不可用。

请告诉我从哪里可以得到这些信息?

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType="a",
                AuthenticationMode = AuthenticationMode.Passive,
                MetadataAddress = String.Format(aadInstance2, tenant2, SignUpSignInPolicyId),
                ClientId = clientId2,
                RedirectUri = redirectUri2,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                CallbackPath= new PathString("/Home/index"),
            });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType = "b",
                AuthenticationMode = AuthenticationMode.Passive,
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                CallbackPath = new PathString("/Home/contact"),
            });


    public void Redirect1()
    {
        HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, "b");
    }

    public void Redirect2()
    {
        HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, "a");
    }

【问题讨论】:

  • 可以添加认证配置的代码吗?
  • 已添加代码。
  • 嗯,您的回调路径可能不应该与现有路由匹配。它们通常类似于/oidc-callback。您确实需要为这两个中间件指定不同的。
  • 我更改了回调路径,但 user.identity.IsAuthenticated 为 false。如果我将 AuthenticationMode 设置为 Active,它确实有效

标签: c# azure azure-active-directory


【解决方案1】:

此问题不是由AuthenticationMode 引起的,您不应指定CallbackPath。如果设置此参数,则 Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationHandler 将仅侦听此地址的帖子。因此,您无法成功处理来自 Azure AD 的重定向。

这里是使用多个OpenId连接OWIN cmets的代码供大家参考:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ClientId = clientId,
    Authority = authority,
    AuthenticationType = "aad1",
    RedirectUri = "http://localhost:2803/",
    AuthenticationMode = AuthenticationMode.Passive,
    PostLogoutRedirectUri= "http://localhost:2803/"
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ClientId = "5efa8abc-13dc-4681-83f5-c6fde071xxxx",
    Authority = authority2,
    AuthenticationType = "aad2",
    RedirectUri = "http://localhost:2803/",
    AuthenticationMode = AuthenticationMode.Passive,
    PostLogoutRedirectUri= "http://localhost:2803/"
});

然后我们可以使用HttpContext.User.Identity.IsAuthenticated检查用户是否登录。

更新

AccountController.cs:

public class AccountController : Controller
{
    public void SignIn(string provider,string ReturnUrl = "/default")
    {
        // Send an OpenID Connect sign-in request.
        if (!Request.IsAuthenticated)
        {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = ReturnUrl }, provider);
            HttpContext.Response.Cookies["provider"].Value = provider;
        }
    }
    public void SignOut()
    {
        var provider = HttpContext.Request.Cookies["provider"].Value;
        Request.Cookies.Clear();
        HttpContext.GetOwinContext().Authentication.SignOut(
            provider, CookieAuthenticationDefaults.AuthenticationType);

    }

    public void EndSession()
    {
        // If AAD sends a single sign-out message to the app, end the user's session, but don't redirect to AAD for sign out.
        HttpContext.GetOwinContext().Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType);
    }
}

登录页面上的登录按钮:

    <input type="button" value="AzureAD-aad1" onclick="location.href='@Url.Action("SignIn", "Account",new { provider="aad1"} )'" />
    <input type="button" value="AzureAD-aad2" onclick="location.href='@Url.Action("SignIn", "Account",new { provider="aad2"} )'" />

这是使用 Fiddler 捕获请求的图:

【讨论】:

  • 这不起作用。如果没有提供callbackPath,就会陷入无限循环。
  • 我的意思是无限循环,天蓝色登录页面重定向到网站根路径,然后网站重定向到被访问的页面(授权)。由于未设置用户身份,它再次重定向到 azure 的登录页面。
  • 我们应该设置不需要授权的redirect_uri。在用户输入他们的用户名/密码后,Azure AD 将重定向(POST)到 URI,OWIN 注释将传递响应并启用用户登录并重定向到他们想要访问的页面。我将发布详细的代码示例和一个图来演示它。
  • 一开始是把CallbackPath去掉了,没用,改写了一些东西后,它工作了(不知道是什么让它工作除了去掉CallbackPath)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2018-07-23
  • 2019-09-24
相关资源
最近更新 更多