Error: “The Id field is required.” while registering new user in database with AspNet.Identity 2.0

孤街浪徒 提交于 2020-01-02 09:58:18

问题


Welcome,

I get the validation error mentioned in topic while creating (registering) new user in my Asp.Net mvc application where I used Asp.Net Identity with my custom ApplicationUser class

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity>
    GenerateUserIdentityAsync(ApplicationUserManager manager)
    {      
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);   
        return userIdentity;
    }

    public virtual string EmailConfirmedChar { get; set; }

    public virtual string PhoneNumberConfirmedChar { get; set; }

    public virtual string TwoFactorEnabledChar { get; set; }

    public virtual string LockoutEnabledChar { get; set; }
}

where my TKey is string, so I expect the Id to be Guid as it is by default.

Of course I implemented my custom UserStore, UserManager etc, in general I inspired by the blog post Customizing the ASP.NET Identity Data Model with the Entity Framework Fluent API

What do I do wrong ? Generaly the same validation error appears while downloaded the sample attached in this blog post.

Relevant or nor, I use Firebird database.


回答1:


IdentityUser class automatically generates GUID when you make a new instance of it. Not IdentityUser<TKey, TLogin, TRole, TClaim> class. However you could easily overcome this problem by generating new GUID in your ApplicationUser class constructor:

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUser()
    {
        Id = Guid.NewGuid().ToString();
    }
    // rest of code
}


来源:https://stackoverflow.com/questions/32671294/error-the-id-field-is-required-while-registering-new-user-in-database-with-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!