Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建UserManager的实例_C#_Asp.net Mvc_Asp.net Identity - Fatal编程技术网

C# 如何创建UserManager的实例

C# 如何创建UserManager的实例,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,我正在尝试学习新的asp.net identity 2.0是如何工作的,但是由于没有什么文档,我遇到了很多障碍 以下代码是根据我阅读的几篇教程编写的: public class CustomRole : IdentityRole<string, CustomUserRole> { public CustomRole() { } public CustomRole(string name) { Name = name; } } public class CustomU

我正在尝试学习新的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());
    }
}
公共类CustomRole:IdentityRole
{
公共CustomRole(){}
公共自定义角色(字符串名称){name=name;}
}
公共类CustomUserRole:IdentityUserRole{}
公共类CustomUserClaim:IdentityUserClaim{}
公共类CustomUserLogin:IdentityUserLogin{}
//定义应用程序用户
公共类应用程序用户:IdentityUser
{
[必需]
公共bool IsActive{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
公共部分类myDbContext:IdentityDbContext
{
静态myDbContext()
{
Database.SetInitializer(null);
}
公共myDbContext()
:base(“Name=myDbContext”)
{
}
公共数据库集测试表{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
添加(新的TestTableMap());
}
}
然后我有了这个代码:

// create the user manager
        UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole,
    CustomUserClaim>(
            new myDbContext()));
//创建用户管理器
UserManager=newusermanager(newuserstore(
新的myDbContext());
我在这条语句中得到一个错误,即参数类型UserStore不能分配给参数类型IUserStore


我在这里错过了什么?

这让我很开心。如果要创建自定义用户和角色,似乎必须创建自己的usermanager和userstore。这是派生的UM(您也可以用与rolemanager相同的方法创建):

公共类应用程序管理员:用户管理员
{
公共应用程序服务器管理器(IUserStore存储)
:基地(商店)
{
}
}
公共类ApplicationUserStore:UserStore
{
公共ApplicationUserStore(ApplicationDbContext上下文)
:基本(上下文)
{
}
}
然后创建UserManager:

ApplicationUserManager um=newApplicationUserManager(newApplicationUserStore(new ApplicationDbContext());
试试这个:

UserManager = new UserManager<ApplicationUser,string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>(new myDbContext()));
UserManager=newusermanager(newuserstore(newmydbcontext());
注意:我对
UserManager
使用了不同的构造,我添加了
string
作为第二种类型,您可以在代码中使用它作为ApplicationUser主键


由于您以这种方式实现了自定义用户/角色/etc,因此需要在整个代码中使用
UserManager
as
UserManager
以字符串形式传递用户PK的类型。

您引入自定义角色/声明/登录类的原因是什么,没有其他功能?如果使用非默认密钥类型实现自定义用户,则需要执行此操作。。。在他的例子中,它是一个
字符串
(尽管它是一个默认值)。一、 例如,想要使用
int
并做了同样的事情。尽管可能还有其他情况下你会这么做