0

我正在开发现有的 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. */);
}

谢谢你。

4

1 回答 1

0

在解决方法上,您可以使用 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

于 2021-12-07T11:04:19.153 回答