值不能为空。参数名称:value

时间:2017-03-11 10:49:42

标签: asp.net asp.net-identity customization

我想在aspnet用户表中添加新字段..i还在帐户视图模型中添加了用户名属性。

 public class ApplicationUser : IdentityUser<int, UserLogin, UserRole, UserClaim>
{
    public DateTime? ActiveUntil;

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
    public string  UserName { get; set; }

}

它给出了以下错误 值不能为空。 参数名称:值

      {
Line 53:             // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
Line 54:             var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
Line 55:             // Add custom user claims here

2 个答案:

答案 0 :(得分:0)

我通过在SecurityStamp字段中放置一个随机字符串来解决这个问题。要生成此字符串,只需使用Guid.NewGuid()

因此,您可以将代码更改为:enter code here

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
    if (string.IsNullOrEmpty(this.SecurityStamp))
    {
        this.SecurityStamp = Guid.NewGuid().ToString().Replace("-", "");
    }

    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    return userIdentity;
}

答案 1 :(得分:0)

我也面临同样的错误。我发现在 Model 中,当我将 [Display] 更改为:

[Display(Name = "")]
public string Budget { get; set; }

取而代之的是:

[Display(Name = "Budget")]
public string Budget { get; set; }

name 的 value 未定义...