[英]How can I add/update a claim in .NET Core 3.1 after I've already authenticated?
在我的場景中,我在用戶和企業之間建立了多對多的關系。 所以,一個企業可以有很多員工,一個用戶可以是很多企業的員工。
在登錄頁面上,我只想顯示 email 和密碼文本框。 一旦他們成功通過身份驗證,我想將他們重定向到一個頁面,該頁面包含他們所雇用的業務的下拉列表。
由於他們已經進行了身份驗證,因此他們的聲明已經被填充。 之后如何添加另一個聲明(他們的 BusinessID)?
來自的答案是指您何時進行身份驗證並從 OAUTH 服務器獲取聲明。 我們不知道您使用的是本地身份表還是 OAUTH,但無論如何。
我包含了一些舊代碼(最后我們以另一種方式實現),但也許可以幫助你。
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Claims;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
namespace Common.Models.Identity {
public class User : IdentityUser<int> {
public bool SendAlertByEmail { get; set; }
public int ClientId { get; set; } = Client.DefaultClientId;
[JsonIgnore, ForeignKey("ClientId")]
public virtual Client Client { get; set; } = null!;
}
public class ApplicationUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<User> {
public ApplicationUserClaimsPrincipalFactory(
UserManager<User> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor) {
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(User user) {
ClaimsIdentity identity;
identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ClientId", user.ClientId.ToString()));
identity.AddClaim(new Claim("ClientDescription", user.Client.Description));
return identity;
}
}
}
在您的 ConfigureServices 中,配置此
#region Configure identity services.AddDefaultIdentity<User>( options => { options.SignIn.RequireConfirmedAccount = true; options.Stores.MaxLengthForKeys = 256; // Max length for key. Regenerate migration if change this }).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultUI().AddDefaultTokenProviders().AddClaimsPrincipalFactory<ApplicationUserClaimsPrincipalFactory>(); services.Configure<IdentityOptions>(options => { // Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; // Lockout settings. options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); options.Lockout.MaxFailedAccessAttempts = 5; options.Lockout.AllowedForNewUsers = true; // User settings. options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+"; options.User.RequireUniqueEmail = false; }); services.ConfigureApplicationCookie(options => { // Cookie settings options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(5); options.LoginPath = "/Identity/Account/Login"; options.AccessDeniedPath = "/Identity/Account/AccessDenied"; options.SlidingExpiration = true; }); #endregion Configure identity
使用 IoC 將您的 ApplicationUserClaimsPrincipalFactory 傳遞給您的“選擇業務/客戶”請求,並將其用於添加聲明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.