【发布时间】:2019-04-15 20:06:08
【问题描述】:
我有这段代码应该为用户设置声明。当我使用身份和默认登录时,它工作正常。但是,当我在另一个应用程序中使用 jwt 作为身份验证时,我没有 ApplicationUser,因为我的 ApplicationUser 存储在另一个对用户进行身份验证的应用程序中。如何自定义此代码以使其与 jwt 一起使用?
private readonly SignInManager<TIdentityUser> _signInManager;
public CustomClaimsCookieSignInHelper(SignInManager<TIdentityUser> signInManager)
{
_signInManager = signInManager;
}
public async Task SignInUserAsync(TIdentityUser user, bool isPersistent, IEnumerable<Claim> customClaims)
{
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(user);
var identity = claimsPrincipal.Identity as ClaimsIdentity;
var claims = (from c in claimsPrincipal.Claims select c).ToList();
var savedClaims = claims;
if (customClaims != null)
{
identity.AddClaims(customClaims);
}
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme,
claimsPrincipal,
new AuthenticationProperties { IsPersistent = isPersistent });
}
我想我的主要目的是在 httpcontext 而不是 cookie 中设置我的用户声明,我想在不使用身份的情况下这样做。
编辑:
我的应用结构
AuthenticationApp(服务器)
- 负责对用户进行身份验证
- 生成和解码 Jwt
- 检查用户是否具有适当的角色并通过rest api返回真/假
MainApp(客户端)
- 对 AuthenticationApp 进行 api 调用
- 根本不使用身份
- 每次我需要检查用户的角色时发送 Jwt
我知道我将能够解码 jwt 客户端。但是,我不知道我可以在哪里存储解码的 jwt 详细信息,以便我可以在视图中使用它。我最初的想法是像使用用户身份的普通应用程序一样使用 Httpcontext。但是,我被上面的代码卡住了。
【问题讨论】:
-
不一样。我正在尝试查看是否有另一种设置 httpcontext 项的方法
标签: asp.net asp.net-mvc asp.net-core jwt