我正在使用ASP创建一个新用户。. NET核心标识如下:
new User {
Email = "john@company.com",
Name = "John"
}
await userManager.CreateAsync(user, "password");
我需要在创建用户时添加Claims。我试着:
new User {
Email = "john@company.com",
Name = "John",
Claims = new List<Claim> { /* Claims to be added */ }
}
但是Claims属性是只读的。
最好的方法是什么?
您可以使用UserManager<YourUser>.AddClaimAsync
方法为您的用户添加声明
var user = new User {
Email = "john@company.com",
Name = "John"
}
await userManager.CreateAsync(user, "password");
await userManager.AddClaimAsync(user, new System.Security.Claims.Claim("your-claim", "your-value"));
或者向用户Claims
集合添加声明
var user = new User {
Email = "john@company.com",
Name = "John"
}
user.Claims.Add(new IdentityUserClaim<string>
{
ClaimType="your-type",
ClaimValue="your-value"
});
await userManager.CreateAsync(user);