I have an ASP.NET Core 3.1 application using EF Core 3.1. How can I seed an user with a few claims? I was unable to find any documentation for this.
Here's a method for seeding a super user you can tweak it to suit you
private void SeedUsers(UserManager<AppUser> userManager)
{
//Check if it's already seeded
if (userManager.FindByEmailAsync(_superUserDetails.Email).Result != null)
return;
//Create User
var user = new AppUser
{
UserName = _superUserDetails.UserName,
Email = _superUserDetails.Email,
FirstName = "Clarke",
LastName = "Kent"
};
//Set password
var userResult = userManager.CreateAsync(user, _superUserDetails.Password).Result;
if (!userResult.Succeeded)
throw new SeedingException(userResult.Errors);
//Add superUser Claim
var superUserClaim = new Claim("MyRoleClaimType", "SuperUser", ClaimValueTypes.String, "MyIssuer");
var roleResult = userManager.AddClaimAsync(user, superUserClaim).Result;
if (!roleResult.Succeeded)
throw new SeedingException(roleResult.Errors);
}//SeedUsers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.