首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建UserManager实例

如何创建UserManager实例
EN

Stack Overflow用户
提问于 2014-04-07 23:16:10
回答 2查看 19K关注 0票数 4

我正在尝试学习新的asp.net标识2.0是如何工作的,但是由于文档很少,我遇到了很多障碍。

下面的代码是基于我阅读过的几个教程编写的:

代码语言:javascript
复制
public class CustomRole : IdentityRole<string, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}

public class CustomUserRole : IdentityUserRole<string> { }
public class CustomUserClaim : IdentityUserClaim<string> { }
public class CustomUserLogin : IdentityUserLogin<string> { }

// define the application user
public class ApplicationUser : IdentityUser<string, CustomUserLogin, CustomUserRole, 
    CustomUserClaim>
{
    [Required]
    public bool IsActive { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;


    }
}

public partial class myDbContext : IdentityDbContext<ApplicationUser, CustomRole, string, 
    CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    static myDbContext()
    {
        Database.SetInitializer<myDbContext>(null);
    }

    public myDbContext()
        : base("Name=myDbContext")
    {
    }

    public DbSet<TestTable> TestTables { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new TestTableMap());
    }
}

然后我有了下面的代码:

代码语言:javascript
复制
// create the user manager
        UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole,
    CustomUserClaim>(
            new myDbContext()));

我在这条语句中得到一个错误,说明参数类型UserStore<-ApplicationUser <-ApplicationUser、CustomRole、string、CustomUserLogin、CustomUserRole、CustomUserClaim->不能分配给参数类型

我在这里错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-08 01:26:38

试试这个:

代码语言:javascript
复制
UserManager = new UserManager<ApplicationUser,string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>(new myDbContext()));

注意:我对UserManager使用了不同的构造,我添加了string作为您在ApplicationUser主键代码中使用的第二种类型

由于您以自己的方式实现了自定义的用户/角色/等等,所以您需要在代码中使用UserManager作为UserManager<ApplicationUser,string>传递给用户PK作为字符串的类型。

票数 10
EN

Stack Overflow用户

发布于 2014-04-07 23:33:39

这对我起了作用。如果您创建自定义用户和角色,则似乎必须创建自己的用户管理器和用户存储。这是派生的UM(您也可以以同样的方式创建rolemanager ):

代码语言:javascript
复制
public class ApplicationUserManager : UserManager<ApplicationUser, string>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
            : base(store)
        {

        }


    }
public class ApplicationUserStore : UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

然后创建UserManager:

代码语言:javascript
复制
ApplicationUserManager um = new ApplicationUserManager(new ApplicationUserStore(new ApplicationDbContext()));
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22924816

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档