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