At the present I am trying to assign a role to a user the role table looks like following
| Role Id | Name | NormalizedName |
|---|---|---|
| 59f5238-818e-40f9-b95a-fc65db67f253 | Parent | Parent |
My User table is
| Id | First Name | Surname |
|---|---|---|
| b3e9d5ec-1ddf-4e57-bf00-1afc2e7f0582 | Parent | One |
The role is also being created fine.
But for some reason when I try to add this user to the roles table I am getting the following.
The Task is cancelled at Microsoft.AspNetCore.Identity.UserManager`1.d__116.MoveNext() at Web.SampleData.d__2.MoveNext() in SampleData.cs:line 118
I am using the code to add to the role manager the user
public static async Task<IdentityResult>
AssignRoles(IServiceProvider services,
string email, string[] roles)
{
IdentityResult result = new IdentityResult();
UserManager<ApplicationUser> _userManager =
services.GetService<UserManager<ApplicationUser>>();
ApplicationUser user = await
_userManager.FindByEmailAsync(email);
result = await _userManager.AddToRolesAsync(user, roles);
return result;
}
The role is being passed as "Parent" and the correct email address of ParentOne@Test.com so why am I getting a task is canceled?
As you see the above user has not been added to the aspnetuseroles table
This is the complete seeding method
public static void CreateParent(IServiceProvider
serviceProvider)
{
var context = new DBContext();//
serviceProvider.GetService<DBContext>();
string[] roles = new string[] { "Parent" };
foreach (string role in roles)
{
var roleStore = new RoleStore<IdentityRole>(context);
if (!context.Roles.Any(r => r.Name == role))
{
roleStore.CreateAsync(new IdentityRole(role));
}
}
var user = new ApplicationUser
{
FirstName = "Parent",
LastName = "One",
Email = "parentone@apps-Test.com",
NormalizedEmail = "parentone@apps-Test.com",
UserName = "parentone@apps-Test.com",
NormalizedUserName = "parentone@apps-Test.com",
PhoneNumber = "+111111111111",
EmailConfirmed = true,
PhoneNumberConfirmed = true,
SecurityStamp = Guid.NewGuid().ToString("D")
};
var db = new DBContext();
if (!db.Users.Any(u => u.UserName == user.UserName))
{
var password = new PasswordHasher<ApplicationUser>);
var hashed = password.HashPassword(user,2
Test12345!");
user.PasswordHash = hashed;
var userStore = new UserStore<ApplicationUser>
(context);
var result = userStore.CreateAsync(user);
}
AssignRoles(serviceProvider, user.Email, roles);
db.SaveChangesAsync();
}
I call it from my web app startup.cs as such
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,IServiceProvider service)
{
SampleData.CreateParent(service);
}
