UserManager does not normalize role name before passing to UserStore
When I read the code for FindByNameAsync, the UserManager will first normalize the user name then pass to UserStore to find the user by the normalized user name.
However, when adding roles to a user, the UserManager only pass the rolename without any normalizations. This pattern is not consistent with the previous case.
And the related issue is that the UserStore in entityframework search for the roles by only using just a upper case comparison. var roleEntity = await Roles.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper(), cancellationToken);
7ba61d6c6f6ce4eaee4c5018e71fa374d99bc868
I tried THIS SeedData example. http://stackoverflow.com/questions/34343599/how-to-seed-users-and-roles-with-code-first-migration-using-identity-asp-net-mvc
and i am very sure, with CORE 1.1.1 Identity framework important things dont work !! users relation Role does not work Usermanager save /assign a role to a user does not work. <= with this i could miss, but i think i initiated a new instance of Usermanager correctly.
after the db is created by Apply migrations, then on a second run i start SeedData. I am very sure:
- Sample username was saved correctly
- 8 Rolenames were saved correctly
- the Relation user to the roles were NOT saved even.
problem the User <-> Role table was not saved, apparently due NULL in field "NormalizedRoleName"
I did create the asp.net web application in YeoMan on Ubuntu, and then i ftp downloaded it to Windows.
Coujld i give somebody the whole sample project.
I did try this of course on MS SQL On Windows
and on My SQL with
On Windows i used the latst CORE version. current. I think this is preview 3
D:\Projects\MembersRating\webtest>dotnet --info .NET Command Line Tools (1.0.1)
Product Information: Version: 1.0.1 Commit SHA-1 hash: 005db40cd1
Runtime Environment: OS Name: Windows OS Version: 6.1.7601 OS Platform: Windows RID: win7-x64 Base Path: C:\Program Files\dotnet\sdk\1.0.1
D:\Projects\MembersRating\webtest>
===== This was my solution temporarily.
-
Save a new Normalized Rolename explicit with toUpper()
-
Make use of UserStore instead of UserManager to assign a given role
-
use ToUpper when assigning a role to a user this way. userStore.AddToRoleAsync(user, role.ToUpper());
public static async Task InitializeAsync(IServiceProvider services)
// JVD 2017-03-28, Temporarily // dont use _userManager to assign/save roles to user. Use userStore to save anassigned Role // See https://github.com/aspnet/Identity/issues/716
string[] roles = new string[] {
"Buyer",
"Owner",
"Administrator",
"Manager",
"Editor",
"Business",
"Seller",
"Subscriber"
};
var roleStore = new RoleStore<IdentityRole>(context);
foreach (string role in roles)
{
if (!context.Roles.Any(r => r.Name == role))
{
var nr = new IdentityRole(role);
nr.NormalizedName = role.ToUpper();
await roleStore.CreateAsync(nr);
}
}
}
public static async Task
var userStore = new UserStore<ApplicationUser>(context);
var passwordHasher = new PasswordHasher<ApplicationUser>();
var itemsValidators = new List<UserValidator<ApplicationUser>>();
itemsValidators.Add(new UserValidator<ApplicationUser>());
var passwordValidators = new List<PasswordValidator<ApplicationUser>>();
passwordValidators.Add(new PasswordValidator<ApplicationUser>());
var keyNormalizer = new LookupNormalizer();
var errors = new IdentityErrorDescriber();
//LoggerFactory logger = new LoggerFactory<ApplicationUserManager<ApplicationUser>>();
ApplicationUser user = await _userManager.FindByEmailAsync(email);
await userStore.FindByEmailAsync(email);
foreach( var role in roles){
//_userManager.AddToRoleAsync(user, role.ToUpper() );
//JVD 2017-03-28, Temporarily dont use _userManager to assign/save roles to user. Use userStore to save anassigned Role
// See https://github.com/aspnet/Identity/issues/716
await userStore.AddToRoleAsync(user, role.ToUpper());
}
await context.SaveChangesAsync();
// var result = await _userManager.AddToRolesAsync(user, roles );
return result;
============= Target framework : .NetCoreApp 1.0
D:\Projects\MembersRating\webtest>dotnet --info .NET Command Line Tools (1.0.1)
Product Information: Version: 1.0.1 Commit SHA-1 hash: 005db40cd1
Runtime Environment: OS Name: Windows OS Version: 6.1.7601 OS Platform: Windows RID: win7-x64 Base Path: C:\Program Files\dotnet\sdk\1.0.1
=======
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0-msbuild3-final" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.0.1" />
Correct, code you are using does create entry in the AspNetRoles table but does not populate the NormalizedName column.
Replace this code:
var roleStore = new RoleStore<IdentityRole>(context);
var nr = new IdentityRole(role);
await roleStore.CreateAsync(nr);
with following:
RoleManager<IdentityRole> roleManager = context.GetService<RoleManager<IdentityRole>>();
IdentityResult result = roleManager.CreateAsync(new IdentityRole(role));