错误:“;Id字段是必需的";同时向AspNet在数据库中注册新用户.Identity 2.0

本文关键字:注册 新用户 数据库 用户 Identity AspNet 字段 Id 错误 quot | 更新日期: 2023-09-27 18:13:25

欢迎光临,

在我的Asp中创建(注册(新用户时,我收到了主题中提到的验证错误。Net mvc应用程序,我在其中使用了Asp。Net Identity与我的自定义ApplicationUser类

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; }
}

其中我的TKey是字符串,所以我希望Id默认为Guid

当然,我实现了我的自定义UserStore,UserManager等,总的来说,我的灵感来自博客文章自定义ASP。NET身份数据模型的实体框架Fluent API

我做错了什么?通常,在下载本博客文章中附带的示例时会出现相同的验证错误。

相关或否,我使用火鸟数据库。

错误:“;Id字段是必需的";同时向AspNet在数据库中注册新用户.Identity 2.0

IdentityUser类在创建新实例时自动生成GUID。IdentityUser<TKey, TLogin, TRole, TClaim>类。然而,您可以通过在ApplicationUser类构造函数中生成新的GUID来轻松克服这个问题:

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