【问题标题】:ASP.NET Core 2.1 Jwt setting custom claimsASP.NET Core 2.1 Jwt 设置自定义声明
【发布时间】: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。但是,我被上面的代码卡住了。

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core jwt


【解决方案1】:

为了在控制器和视图之间共享身份信息,您可以通过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?}");
        });
    }
    

    }

【讨论】:

    猜你喜欢
    • 2018-07-01
    • 2018-05-08
    • 2022-06-30
    • 2019-04-14
    • 2020-02-09
    • 2019-01-24
    • 2018-07-05
    • 2019-10-01
    • 2020-01-17
    相关资源
    最近更新 更多