我有这个代码应该为用户设置声明 . 当我使用身份和默认登录时,它工作正常 . 但是,当我在另一个应用程序中使用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返回true / false
MainApp(客户端)
-
对AuthenticationApp进行api调用
-
根本不使用身份
-
每次我需要检查用户的角色时发送Jwt
据我所知,我将能够解码jwt客户端 . 但是,我不知道在哪里可以存储解码的jwt细节,以便我可以在视图中使用它 . 我最初的想法是使用Httpcontext,就像用户身份的普通应用程序一样 . 但是,我坚持使用上面的代码 .
1 回答
要在Controller和View之间共享身份信息,您可以通过
HttpContext.SignInAsync
签署用户信息 .请尝试以下步骤以满足您的要求:
Startup.cs
中注册身份验证}