【问题标题】:Inherit from IdentityUser I get an error on UserManager<User>从 IdentityUser 继承我在 UserManager<User> 上收到错误
【发布时间】:2015-01-12 02:28:35
【问题描述】:

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

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

我自己的dbContext

public class EFDbContext : IdentityDbContext, IUnitOfWork

我自己的User 班级。

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

但是当我这样做时:

UserManager&lt;User&gt; _userManager;

我收到此错误:

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

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

我该如何解决这个错误?

更新

更新 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”到“M​​icrosoft.AspNet.Identity.EntityFramework.IdentityUser”的隐式引用的转换。
  3. 参数 1:无法从“Microsoft.AspNet.Identity.EntityFramework.UserStore”转换为“Microsoft.AspNet.Identity.IUserStore”

如何解决这个新错误?

【问题讨论】:

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


【解决方案1】:

使用other UserManager

UserManager<User, long> _userManager;

您使用的是this UserManager

代表用户的用户管理器,其中用户的主键是字符串类型。

【讨论】:

  • 感谢您的回答。现在,我在这里遇到了同样的问题:_userManager = new UserManager&lt;User, long&gt;(new UserStore&lt;User&gt;(_ctx));.
  • 你的用户存储应该把你的上下文作为它的类型参数
【解决方案2】:

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

这是我的上下文类,注意我使用字符串作为键:

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

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

并且需要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 文件。

如果您能阅读德语,请查看blog
它基本上描述了你想要做什么。 (虽然采用 db-first 方法)
也看他的video(他的英语说得很好)。
还有part 2 也是。

我希望这可以为您提供足够的信息来继续前进:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2019-08-20
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多