Azure Active Directory 和 ASP.NET Core 的混合身份验证和单点登录

Posted

技术标签:

【中文标题】Azure Active Directory 和 ASP.NET Core 的混合身份验证和单点登录【英文标题】:Mixed authentication and single sign on with Azure Active Directory and ASP.NET Core 【发布时间】:2022-01-11 18:43:52 【问题描述】:

我正在开发现有的 ASP.NET Core 3.1 Web API。现有的认证方案是 JWT。有一项新要求是我们支持使用 Azure Active Directory 进行单点登录。

所以,我认为我要遵循的策略是使用 Microsoft 对用户进行身份验证,并且当我的 signin-oidc URL 回调被调用时,我将使用 Microsoft 的令牌来匹配我的应用程序的用户。然后,我将按照应用程序已经执行的方式生成现有的 JWT,以授权进一步的操作。因此,我想通过 Microsoft 进行身份验证,然后继续使用旧的授权方案。

现在问题来了。据我了解,signin-oidc URL 不是您自己定义的,而是隐藏在 Microsoft.Identity.Web 中的某个位置。

尽管如此,根据上面的描述,我得出结论,我必须定义它。那么,实施它的正确方法是什么?我在下面的实现有效,但有些东西告诉我我错过了一些对安全至关重要的东西。

[HttpPost("signin-oidc")]
[AllowAnonymous]
[RequireHttps]
public IActionResult AuthorizeAzureAD([FromForm] OIDCForm form)

    var jwtHandler = new JwtSecurityTokenHandler();
    var token = jwtHandler.ReadJwtToken(form.id_token);
    var claims = token.Claims.ToArray();

    // Use the nonce claim to make sure that this callback is initiated by me.
    var guidClaim = claims.FirstOrDefault(c => c.Type == "nonce");

    if (guidClaim == null || !_azureGuids.TryRemove(guidClaim.Value, out string guid))
        return Unauthorized();

    return Ok(/* return the old JWT to the application for further communication. */);

谢谢。

【问题讨论】:

【参考方案1】:

在解决方法中,您可以使用 OpenID 连接进行 SSO

这里有一些文档如何使用 OpenID Connect 将 SSO 添加到 Azure 中已发布的 Web 应用程序。您可以完全按照这些步骤构建他们的演示版本进行测试,或者按照他们在底部显示如何在您自己的应用程序中实现 SSO 的步骤。

https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect

有关详细信息,请参阅此文档:

1) https://midnightprogrammer.net/post/single-sign-on-in-aspnet-core-with-azure-active-directory/

2) https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp

【讨论】:

谢谢。我见过这些,但它们与我的场景非常不同。

以上是关于Azure Active Directory 和 ASP.NET Core 的混合身份验证和单点登录的主要内容,如果未能解决你的问题,请参考以下文章

Active Directory 从浏览器访问 Azure 存储

在本地和 Azure 上使用 Active Directory 进行身份验证

如何从 Azure Active Directory 检索用户信息

Azure-Azure Active Directory(AAD)

Azure Active Directory Safari 重定向问题

text Azure Active Directory身份验证