PasswordSignInAsync。 ArgumentNullException:值不能为null

时间:2016-02-15 10:16:26

标签: c# asp.net-mvc entity-framework asp.net-identity

我正在使用以下代码登录用户:

public async Task<RepositoryResponse> PasswordSignInAsync(string userName, string password, bool isPersistent,
    bool lockoutOnFailure)
{
    var signInResult = await _signInManager.PasswordSignInAsync(userName, password, isPersistent, lockoutOnFailure);

    return GetResponseBySignInResult(signInResult);
}

我收到一个来自PasswordSignInAsync方法的异常,如下所示:

  

ArgumentNullException:值不能为null。参数名称:   entityType Microsoft.Data.Entity.Utilities.Check.NotNull [T](T值,   String parameterName)

我正在使用Identity 3,MVC6和Entity Framework 7。

4 个答案:

答案 0 :(得分:1)

我有一个类似的问题让我疯了很长时间......值得检查一下你的用户有一个不是null的SecurityStamp(将它设置为随机字符串将起作用)。身份似乎没有问题将其存储为数据库中的空字段,但只要您需要执行任何操作,例如登录就会引发此异常。

那是我的问题,希望它对你来说一样,而且有效! (尽管问了很久以前这个问题)

与此同时,如果任何对身份有更多认识的人都会遇到这种情况并且可以解释安全标志可以为空的优点,那么可能值得一两个投票。

答案 1 :(得分:1)

如果您在ApplicationUser的类中设置Claim值,如下所示。

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            userIdentity.AddClaim(new Claim("FirstName", this.FirstName));
            userIdentity.AddClaim(new Claim("LastName", this.LastName));
            userIdentity.AddClaim(new Claim("FullName", this.FullName));
            userIdentity.AddClaim(new Claim("MobileNo", this.PhoneNumber)); 
            userIdentity.AddClaim(new Claim("Id", this.Id));
            return userIdentity;
        }

任何一个的值都是null,所以签名时会抛出这种异常, 然后请确保您的声明的值在Db“AspNetUsers”表

中具有价值

由于

Dushmantha

答案 2 :(得分:1)

在我的情况下,此问题是由以下事实引起的:我的用户模型(从IdentityUser继承的用户模型)覆盖了Id属性。从此模型中删除行public int Id { get; set; }可解决此问题。

答案 3 :(得分:0)

我遇到了这个问题,发现其中一条记录中的ClaimValue列在数据库的AspNetRoleClaims表中具有空值。解决该问题后,便可以登录。