AspNetIdentity
AspNetIdentity copied to clipboard
ClaimsIdentityFactory.CreateAsync Null Reference Exception
I am trying to use userManager.CreateIdentityAsync method, but I get a NRE everytime.
After debugging the code for AspNetIdentity, I have noticed that the NRE was thrown when calling CreateAsync in ClaimsIdentityFactory class method.
More precisely, the exception is thrown when iterating over the list of roles, but in my particular case it is null
and so it throws. I have added a comment in the code to indicate exactly where the exception is thrown:
/// <summary>
/// Create a ClaimsIdentity from a user
/// </summary>
/// <param name="manager"></param>
/// <param name="user"></param>
/// <param name="authenticationType"></param>
/// <returns></returns>
public virtual async Task<ClaimsIdentity> CreateAsync(UserManager<TUser, TKey> manager, TUser user,
string authenticationType)
{
if (manager == null)
{
throw new ArgumentNullException("manager");
}
if (user == null)
{
throw new ArgumentNullException("user");
}
var id = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType);
id.AddClaim(new Claim(UserIdClaimType, ConvertIdToString(user.Id), ClaimValueTypes.String));
id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String));
id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String));
if (manager.SupportsUserSecurityStamp)
{
id.AddClaim(new Claim(SecurityStampClaimType,
await manager.GetSecurityStampAsync(user.Id).WithCurrentCulture()));
}
if (manager.SupportsUserRole)
{
IList<string> roles = await manager.GetRolesAsync(user.Id).WithCurrentCulture(); // <-- roles is null
foreach (string roleName in roles) // <-- NRE thrown here
{
id.AddClaim(new Claim(RoleClaimType, roleName, ClaimValueTypes.String));
}
}
if (manager.SupportsUserClaim)
{
id.AddClaims(await manager.GetClaimsAsync(user.Id).WithCurrentCulture());
}
return id;
}
I am not sure why the list of roles is null
though. Is this bug or is there something I am missing there? Thanks.
NuGet:
- package: Microsoft.AspNet.Identity.Core
- version: 2.2.1