In a ASP.NET Core 7 application have extended IdentityUser
like this:
在ASP.NET Core 7应用程序中,可以像这样扩展IdentityUser:
public class AppUser : IdentityUser
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
I created a DB conext:
我创建了一个数据库上下文:
public class ApplicationDbContext : IdentityDbContext<AppUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
I'm configuring my host to use it:
我正在配置我的主机以使用它:
services
.AddDefaultIdentity<AppUser>(
options
=> options.SignIn.RequireConfirmedAccount = true
)
.AddEntityFrameworkStores<ApplicationDbContext>();
When debugging the running application, Request.User
is of type ClaimsPrincipal
which holds the Claims including FirstName
and LastName
as expected.
在调试运行中的应用程序时,Request.User的类型为ClaimsPrincipal,它按预期保存包括FirstName和LastName在内的Claims。
However, when I'm using UserManager<AppUser>
and SignInManager<AppUser>
to create a ClaimsPrincipal
, the claims are missing (not all Claims are missing, though).
然而,当我使用UserManager
和SignInManager
创建Claimsain时,声明丢失了(但并不是所有声明都丢失)。
This is how I'm creating the ClaimsPrincipal
within a test.
这就是我在测试中创建ClaimsMaster的方法。
_host
is using the exact same configuration as the running application does.
_host正在使用与正在运行的应用程序完全相同的配置。
var userManager =
_host.Services.GetService<UserManager<AppUser>>();
var appUser = userManager
.Users
.FirstOrDefault(u => u.Id == user.Sub);
var signInManager =
_host.Services.GetService<SignInManager<AppUser>>();
_claimsPrincipal = await signInManager.CreateUserPrincipalAsync(appUser);
更多回答
优秀答案推荐
I'm using an IClaimsTransformation
which adds the missing claims.
我使用了一个IClaimsTransformation,它添加了缺失的声明。
IClaimsTransformation
s are only run on actual authentication which is not the case in my test.
IClaimsTransform仅在实际身份验证时运行,而在我的测试中并非如此。
更多回答
我是一名优秀的程序员,十分优秀!