【问题标题】:How do i create an instance of UserManager如何创建 UserManager 的实例
【发布时间】:2014-04-08 09:32:06
【问题描述】:

我正在尝试了解新的 asp.net identity 2.0 是如何工作的,但是由于文档很少,我遇到了很多绊脚石。

以下代码基于我阅读的几个教程:

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

然后我有这个代码:

// create the user manager
        UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole,
    CustomUserClaim>(
            new myDbContext()));

我在此语句中收到错误,说明参数类型 UserStore 不可分配给参数类型 IUserStore

我在这里错过了什么?

【问题讨论】:

  • 您为什么要引入自定义角色/声明/登录类,而没有其他功能?
  • 如果您使用非默认键类型实现自定义用户,则需要这样做......在他的情况下,它是string(尽管它是默认值)。例如,我想使用int 并做了同样的事情。尽管您可能会在其他情况下想要这样做

标签: c# asp.net-mvc asp.net-identity


【解决方案1】:

试试这个:

UserManager = new UserManager<ApplicationUser,string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>(new myDbContext()));

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

由于您以您的方式实现了自定义用户/角色/等,因此您需要在整个代码中使用 UserManager 作为 UserManager&lt;ApplicationUser,string&gt; 以将用户 PK 的类型作为字符串传递。

【讨论】:

    【解决方案2】:

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

    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:

    ApplicationUserManager um = new ApplicationUserManager(new ApplicationUserStore(new ApplicationDbContext()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多