首页 文章

ASP.NET Core 2.1 Jwt设置自定义声明

提问于
浏览
0

我有这个代码应该为用户设置声明 . 当我使用身份和默认登录时,它工作正常 . 但是,当我在另一个应用程序中使用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 回答

  • 0

    要在Controller和View之间共享身份信息,您可以通过 HttpContext.SignInAsync 签署用户信息 .

    请尝试以下步骤以满足您的要求:

    • 控制器动作
    public async Task<IActionResult> Index()
    {
        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "edward"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "edward zhou"));
        //add your own claims from jwt token
        var principal = new ClaimsPrincipal(identity);
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { IsPersistent = true });            
        return View();
    }
    
    • 查看
    @foreach (var item in Context.User.Claims)
    {
       <p>@item.Value</p> 
    };
    
    • 要使上述代码有效,请在 Startup.cs 中注册身份验证
    public class Startup
    {
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; }
    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {   
         //your rest code     
    
    
         services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
    }
    
    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //your rest code
        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
    

    }

相关问题