繁体   English   中英

从IdentityUser继承我在UserManager上收到错误<User>

[英]Inherit from IdentityUser I get an error on UserManager<User>

我正在使用.NET Framework 4.5.1和Asp.Net Identity 2.1.0开发Web Api 2.2应用程序。

我不确定我在做什么,但我想将我的数据库与ASP.NET Identity数据库合并,我已经这样做了:

我自己的dbContext

public class EFDbContext : IdentityDbContext, IUnitOfWork

我自己的User类。

public class User : 
   IdentityUser<long,
                IdentityUserLogin<long>,
                IdentityUserRole<long>,
                IdentityUserClaim<long>
               >

但是当我这样做时:

UserManager<User> _userManager;

我收到此错误:

Data.Models.User类型不能用作TUser类型的参数。 Data.Models.UserMicrosoft.AspNet.Identity.IUser<string>没有任何显式转换。

我这样做是因为我想让IdentityUser.Id long而不是string

我该如何解决这个错误?

UPDATE

用以下内容更新UserManager后:

UserManager<User, long> _userManager;

我在这里得到三个错误:

EFDbContext_ctx = context as EFDbContext;
_userManager = new UserManager<User, long>(new UserStore<User>(_ctx));
  1. “Microsoft.AspNet.Identity.UserManager.UserManager(Microsoft.AspNet.Identity.IUserStore)”方法重载的最佳匹配有一些无效的参数 -
  2. “Data.Models.User”类型不能用作“TUser”类型的参数或通用方法“Microsoft.AspNet.Identity.EntityFramework.UserStore”。 没有从'Data.Models.User'到'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'的隐式引用的转换。
  3. 参数1:无法从“Microsoft.AspNet.Identity.EntityFramework.UserStore”转换为“Microsoft.AspNet.Identity.IUserStore”

我该如何解决这个新错误?

使用其他UserManager

UserManager<User, long> _userManager;

您正在使用此UserManager

表示用户的主键为string类型的用户的用户管理器。

使用自己的User类扩展时,必须提供Identity使用的所有类型。

这是我的上下文类,请注意我使用string作为键:

public class EarlyDetectionContext  
   : IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim>

当然,您还需要为角色userlogin,userclaim和userroles创建类。 这是我的:

public class EDClaim : IdentityUserClaim<string>
{
}  
public class EDLogin : IdentityUserLogin<string>
{
}
[Table("Roles", Schema = "ED")]
public class Role : IdentityRole<string, RoleUserAssociation>
{
    [Required, Column("DisplayName")]
    public string DisplayName { get; set; }

    [Required, Column("Created")]
    public DateTime Created { get; set; }

    [Required, Column("Updated")]
    public DateTime Updated { get; set; }

    [Required, Column("Deleted")]
    public bool Deleted { get; set; }
}  
[Table("RoleUserAssociations", Schema = "ED")]
public class RoleUserAssociation : IdentityUserRole<string>
{
    //
    //  Due to Identity 2 the Id needs to of string type
    //
    [Key]
    [Required]
    public string ID { get; set; }

    //[Required, Column("User_Id")]
    //public User User { get; set; }

    //[Required, Column("Role_Id")]
    //public Role Role { get; set; }

    [Required, Column("Created")]
    public DateTime Created { get; set; }

    [Required, Column("Updated")]
    public DateTime Updated { get; set; }

    [Required, Column("Deleted")]
    public bool Deleted { get; set; }
}  
[Table("Users", Schema = "ED")]
public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim>
{
    // This is needed for the new Identity 2 framework
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> 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
        return userIdentity;
    }

    [Required, Column("Municipality_Id")]
    public Municipality Municipality { get; set; }

    public string PasswordSalt { get; set; }

    [Required, Column("FirstName")]
    public string FirstName { get; set; }

    [Required, Column("LastName")]
    public string LastName { get; set; }

    [Column("Title")]
    public string Title { get; set; }

    [Required, Column("Created")]
    public DateTime Created { get; set; }

    [Required, Column("Updated")]
    public DateTime Updated { get; set; }

    [Required, Column("Deleted")]
    public bool Deleted { get; set; }

    public bool Complete { get; set; }

    [Required]
    public DateTime Activated { get; set; }
}

你需要在每个类上指定键类型,在我的例子中是一个字符串,在你的情况下为long。 另请注意我的声明和登录类是空的。 没关系,我不需要在我的情况下添加任何功能或属性,但我确实需要指定它们。

并且需要User类中的GenerateUserIdentityAsync方法。

在Startup.Auth.cs文件中,您需要添加:

// Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(EarlyDetectionContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);  

您还需要OWIN启动类(可以通过右键单击 - 添加 - 新项添加)和Identity.cs文件。

如果您可以阅读德语,请查看此博客
它基本上描述了你想要做的事情。 (虽然有db-first方法)
还要看他的视频 (他说英语很好)。
其中第2部分也是如此。

我希望这能给你足够的信息继续前进:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM