创建新用户时添加声明
Add claims when creating a new user
我正在使用 ASP.NET 核心身份创建一个新用户,如下所示:
new User {
Email = "john@company.com",
Name = "John"
}
await userManager.CreateAsync(user, "password");
我需要在创建用户时添加一个声明。我试过了:
new User {
Email = "john@company.com",
Name = "John",
Claims = new List<Claim> { /* Claims to be added */ }
}
但声明 属性 是只读的。
最好的方法是什么?
您可以使用 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);
我正在使用 ASP.NET 核心身份创建一个新用户,如下所示:
new User {
Email = "john@company.com",
Name = "John"
}
await userManager.CreateAsync(user, "password");
我需要在创建用户时添加一个声明。我试过了:
new User {
Email = "john@company.com",
Name = "John",
Claims = new List<Claim> { /* Claims to be added */ }
}
但声明 属性 是只读的。
最好的方法是什么?
您可以使用 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);