【问题标题】:cannot create an instance using UserStore无法使用 UserStore 创建实例
【发布时间】:2016-06-21 21:24:58
【问题描述】:

我刚刚使用 UserStore 定义了一个实例,如下所示。

var store = new UserStore<ApplicationUser>(new ProjectEntities());

但出现以下错误

类型“project_name.Models.ApplicationUser”不能用作类型 泛型类型或方法“UserStore”中的参数“TUser”。 没有隐式引用转换 'project_name.Models.ApplicationUser' 到 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'。

这里是我如何在IdentityModel.cs 中定义ApplicationUser

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

    // custom User properties/code here
    public string Full_Name { get; set; }
    public string Gender { get; set; }

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

【问题讨论】:

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


    【解决方案1】:

    我认为你做错了什么错误很明显

    您正在尝试传递ApplicationUser,但UserStore 要求您使用Microsoft.AspNet.Identity.EntityFramework.IdentityUse 的类型,您可能应该从Microsoft.AspNet.Identity.EntityFramework.IdentityUse 扩展/继承ApplicationUser

    类似

    public class ApplicationUser : IdentityUser
    

    【讨论】:

    • ApplicationUserIdentityModels.cs 文件中存在这样的public class ApplicationUser : IdentityUser&lt;string,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim&gt; {
    【解决方案2】:

    我不确定您为什么使用 IdentityUser&lt;string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim&gt; 而不是 IdentityUser,正如 COLD TOLD 指出的那样,因为该签名的最常见用途是使用 INT 键而不是字符串。使用 IdentityUser,你会默认得到字符串键。

    因此,假设您有其他原因使用该签名,您将需要覆盖整个身份堆栈:IdentityUser、IdentityUserRole、IdentityRole、IdentityUserClaim、IdentityUserLogin、IdentityDbContext、UserStore 和 RoleStore。

    所以 UserStore 将是:

    public class ApplicationUserStore : 
        UserStore<ApplicationUser, ApplicationRole, int,
        ApplicationUserLogin, ApplicationUserRole, 
        ApplicationUserClaim>, IUserStore<ApplicationUser, int>, 
        IDisposable
    {
        public ApplicationUserStore() : this(new IdentityDbContext())
        {
            base.DisposeContext = true;
        }
    
        public ApplicationUserStore(DbContext context)
            : base(context)
        {
        }
    }
    

    http://johnatten.com/2014/07/13/asp-net-identity-2-0-extending-identity-models-and-using-integer-keys-instead-of-strings/

    【讨论】:

    • 在我的IdentityModels.cs 文件中我有ApplicationUserStore as follows 但是一旦我用你的代码sn-p 替换它得到6 个编译时错误,首先是这样的The type 'project_name.Models.ApplicationUserRole' cannot be used as type parameter 'TUserRole' in the generic type or method 'UserStore&lt;TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim&gt;'.
    • 您必须覆盖所有 8 个身份类别(或使用非泛型 IdentityUser)。
    猜你喜欢
    • 2019-11-21
    • 2020-11-22
    • 1970-01-01
    • 2020-01-16
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多