Reputation: 1104
I have a MVC web site that uses custom identity with my own tables. Most everything is working fine... adding users, roles, etc.
Now I'm adding a user to a role through the user manager like this:
var result = um.AddToRole(userID, roleName);
the "um" is my UserStore interface. Before it calls the AddToRole method it calls the FindByIdAsync method passing in a null value for the userID. Not good. That breaks the entire process.
Microsoft Identity decides how those routines are called behind the scenes, and I can't figure out why a null is getting passed. I'm guessing that I have something wrong in part of the UserStore implementation, but I'm having trouble finding it.
What calls the FindByIdAsync method when I try to AddToRole????
Upvotes: 1
Views: 794
Reputation: 12132
The AddToRole
method is an extension method defined as:
/// <summary>
/// Add a user to a role
///
/// </summary>
/// <param name="manager"/><param name="userId"/><param name="role"/>
/// <returns/>
public static IdentityResult AddToRole<TUser, TKey>(this UserManager<TUser, TKey> manager, TKey userId, string role) where TUser : class, IUser<TKey> where TKey : IEquatable<TKey>
{
if (manager == null)
throw new ArgumentNullException("manager");
return AsyncHelper.RunSync<IdentityResult>((Func<Task<IdentityResult>>) (() => manager.AddToRoleAsync(userId, role)));
}
in UserManagerExtensions
. As you can see it's just calling AddToRoleAsync
which in turn is defined as:
/// <summary>
/// Add a user to a role
///
/// </summary>
/// <param name="userId"/><param name="role"/>
/// <returns/>
public virtual async Task<IdentityResult> AddToRoleAsync(TKey userId, string role)
{
this.ThrowIfDisposed();
IUserRoleStore<TUser, TKey> userRoleStore = this.GetUserRoleStore();
TUser user = await TaskExtensions.WithCurrentCulture<TUser>(this.FindByIdAsync(userId));
if ((object) user == null)
throw new InvalidOperationException(string.Format((IFormatProvider) CultureInfo.CurrentCulture, Resources.UserIdNotFound, new object[1]
{
(object) userId
}));
IList<string> userRoles = await TaskExtensions.WithCurrentCulture<IList<string>>(userRoleStore.GetRolesAsync(user));
IdentityResult identityResult;
if (userRoles.Contains(role))
{
identityResult = new IdentityResult(new string[1]
{
Resources.UserAlreadyInRole
});
}
else
{
await TaskExtensions.WithCurrentCulture(userRoleStore.AddToRoleAsync(user, role));
identityResult = await TaskExtensions.WithCurrentCulture<IdentityResult>(this.UpdateAsync(user));
}
return identityResult;
}
in UserManager
. So if this call:
TUser user = await TaskExtensions.WithCurrentCulture<TUser>(this.FindByIdAsync(userId));
is passing null for userID then by looking at the call chain it could only be because you're passing in a null value for userID.
So to answer your question:
What calls the FindByIdAsync method when I try to AddToRole????
A: UserManager.AddToRoleAsync
Upvotes: 1