简体   繁体   English

UserManager.AddToRole将空用户ID传递给FindByIdAsync

[英]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.

相关问题 UserManager.AddToRole 不起作用 - 外键错误 - UserManager.AddToRole not working - Foreign Key error UserManager.AddToRole对于Facebook登录默默失败 - UserManager.AddToRole failing silently for facebook logins 身份UserManager.AddToRole引发异常 - Identity UserManager.AddToRole throws exception MVC5:UserManager.AddToRole():“将用户添加到角色时出错:找不到UserId”? - MVC5: UserManager.AddToRole(): “Error Adding User to Role: UserId not found”? 如果FindByIdAsync,Id为空怎么办 - What if the FindByIdAsync, Id is null 单元测试:用户管理器<applicationuser>模拟设置 FindByIdAsync 返回 NULL</applicationuser> - UnitTests: UserManager<ApplicationUser> Mock Setup FindByIdAsync Returns NULL UserManager.FindByIdAsync(User.Identity.GetUserId())是否缓存? - Does UserManager.FindByIdAsync(User.Identity.GetUserId()) Cache? _userManager.GetUserAsync(User) 返回 null - _userManager.GetUserAsync(User) returns null 包括ASP.Net Identity 2.0 UserManager.Users.ToListAsync和UserManager.FindByIdAsync上的属性 - Include property on ASP.Net Identity 2.0 UserManager.Users.ToListAsync and UserManager.FindByIdAsync UserManager.GetUserAsync(User)是否可以在具有Authorize属性的类中返回null? - Can UserManager.GetUserAsync(User) return null in a class with Authorize attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM