如何创建UserManager的实例 [英] How do i create an instance of UserManager

查看:214
本文介绍了如何创建UserManager的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习新的asp.net身份2.0的工作方式,但是几乎没有文档,我遇到了很多绊脚石.

I am trying to learn how the new asp.net identity 2.0 works, but with little documentation I am hitting quite a few stumbling blocks.

以下是我阅读过的一些教程的基础上的这段代码:

I have this code below based off of a couple of tutorials that I have read:

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< -ApplicationUser,CustomRole,字符串,CustomUserLogin,CustomUserRole, 无法将CustomUserClaim-> 分配给参数类型 IUserStore< -ApplicationUser->

I get an error in this statement saying the argument type UserStore<-ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim-> is not assignable to the parameter type IUserStore<-ApplicationUser->

我在这里想念什么?

推荐答案

尝试一下:

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

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

note I use a different construction for UserManager, I added string as a second type that you use in your code for ApplicationUser primary key

由于您已经按照自定义的方式实现了自定义user/roles/etc,因此您需要在整个代码中将UserManager用作UserManager<ApplicationUser,string>,以将User PK的类型作为字符串传递.

Since you implemented custom user/roles/etc the way you did, you'll need to use UserManager as UserManager<ApplicationUser,string> throughout your code to pass in the type for User PK as a string.

这篇关于如何创建UserManager的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆