UserManager.AddToRole 将空用户 ID 传递给 FindByIdAsync
UserManager.AddToRole passes null user id to FindByIdAsync
我有一个 MVC 网站,它对我自己的表使用自定义标识。大多数一切都运行良好...添加用户、角色等。
现在我通过用户管理器向角色添加用户,如下所示:
var result = um.AddToRole(userID, roleName);
"um" 是我的 UserStore 界面。在调用 AddToRole 方法之前,它调用 FindByIdAsync 方法,为 userID 传入空值。不好。这打破了整个过程。
Microsoft Identity 决定如何在幕后调用这些例程,我不明白为什么传递 null。我猜我在 UserStore 实现的一部分有问题,但我找不到它。
当我尝试 AddToRole 时,什么调用了 FindByIdAsync 方法????
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)));
}
在 UserManagerExtensions
中。如您所见,它只是调用 AddToRoleAsync
,而 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;
}
在 UserManager
中。所以如果这个调用:
TUser user = await TaskExtensions.WithCurrentCulture<TUser>(this.FindByIdAsync(userId));
正在为 userID 传递 null 然后通过查看调用链可能只是因为您为 userID 传递了 null 值。
所以回答你的问题:
What calls the FindByIdAsync method when I try to AddToRole????
A: UserManager.AddToRoleAsync
我有一个 MVC 网站,它对我自己的表使用自定义标识。大多数一切都运行良好...添加用户、角色等。
现在我通过用户管理器向角色添加用户,如下所示:
var result = um.AddToRole(userID, roleName);
"um" 是我的 UserStore 界面。在调用 AddToRole 方法之前,它调用 FindByIdAsync 方法,为 userID 传入空值。不好。这打破了整个过程。
Microsoft Identity 决定如何在幕后调用这些例程,我不明白为什么传递 null。我猜我在 UserStore 实现的一部分有问题,但我找不到它。
当我尝试 AddToRole 时,什么调用了 FindByIdAsync 方法????
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)));
}
在 UserManagerExtensions
中。如您所见,它只是调用 AddToRoleAsync
,而 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;
}
在 UserManager
中。所以如果这个调用:
TUser user = await TaskExtensions.WithCurrentCulture<TUser>(this.FindByIdAsync(userId));
正在为 userID 传递 null 然后通过查看调用链可能只是因为您为 userID 传递了 null 值。
所以回答你的问题:
What calls the FindByIdAsync method when I try to AddToRole????
A: UserManager.AddToRoleAsync