簡體   English   中英

ASP.NET Core 2.1 Jwt設置自定義聲明

[英]ASP.NET Core 2.1 Jwt setting custom claims

我有應該為用戶設置聲明的這段代碼。 當我使用身份和默認登錄名時,它工作正常。 但是,當我在另一個應用程序中將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。 但是,我堅持上面的代碼。

為了在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?}"); }); } 

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM