具有外部 Windows 身份验证可能性的 ASP.NET Core JWT 承载身份验证 第二次调用 api 生成令牌

问题描述

我想将 ASP.NET Core JWT 不记名身份验证与 Windows 身份验证结合使用。这可能吗?我还没有找到实现它的正确方法。

当前实现

我目前的实现是通过 Windows 进行身份验证,将身份验证数据保存在临时 cookie 中。然后我重定向到另一个操作,检查临时 cookie,如果外部方案(在我的情况下为 Windows)成功通过身份验证,则生成 JWT 令牌并清除临时 cookie。

第二次调用 api 生成令牌

var result = await HttpContext.AuthenticateAsync(AuthenticationDefaults.ExternalCookiesScheme);
if (result?.Succeeded != true) {
  throw new Exception("External authentication error");
}
string jwtToken = JwtFactory.GenerateJSONWebToken(result.Principal.Claims);

await HttpContext.SignOutAsync(AuthenticationDefaults.ExternalCookiesScheme);

Response.Headers.Add("JWTToken",jwtToken);

return new OkResult(); 

我不确定使用临时 cookie 是否正确。

我该如何实施?或者在临时 cookie 中保存外部身份验证数据是正确的,我的实现没问题?

更新

我找到了关于它的 Dino Esposito 文章 => https://docs.microsoft.com/en-us/archive/msdn-magazine/2017/september/cutting-edge-cookies-claims-and-authentication-in-asp-net-core#external-authentication

有用于外部身份验证的 cookie,所以现在我对使用我的实现更有信心了。

无论如何,我会很高兴看到更多使用 jwt 令牌生成的外部 Windows 身份验证选项

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)