[英]UserManager.AddToRole passes null user id to FindByIdAsync
I have a MVC web site that uses custom identity with my own tables. 我有一个MVC网站,该网站在我自己的表中使用自定义身份。 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. “ um”是我的UserStore界面。 Before it calls the AddToRole method it calls the FindByIdAsync method passing in a null value for the userID.
在调用AddToRole方法之前,它将调用FindByIdAsync方法,并为用户ID传递一个空值。 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. Microsoft Identity决定如何在后台调用这些例程,但我不知道为什么会传递null。 I'm guessing that I have something wrong in part of the UserStore implementation, but I'm having trouble finding it.
我猜我在UserStore实现中有问题,但是我找不到它。
What calls the FindByIdAsync method when I try to AddToRole???? 当我尝试AddToRole时,什么叫FindByIdAsync方法?
The AddToRole
method is an extension method defined as: AddToRole
方法是一种扩展方法,定义为:
/// <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
. 在
UserManagerExtensions
。 As you can see it's just calling AddToRoleAsync
which in turn is defined as: 如您所见,它只是调用
AddToRoleAsync
,而后者又定义为:
/// <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
. 在
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. 为userID传递null,然后通过查看调用链,只能是因为您要为userID传递null值。
So to answer your question: 因此,回答您的问题:
What calls the FindByIdAsync method when I try to AddToRole????
当我尝试AddToRole时,什么叫FindByIdAsync方法?
A: UserManager.AddToRoleAsync
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.