我正在尝试学习新的asp.net标识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<-ApplicationUser <-ApplicationUser、CustomRole、string、CustomUserLogin、CustomUserRole、CustomUserClaim->不能分配给参数类型
我在这里错过了什么?
发布于 2014-04-08 01:26:38
试试这个:
UserManager = new UserManager<ApplicationUser,string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>(new myDbContext()));注意:我对UserManager使用了不同的构造,我添加了string作为您在ApplicationUser主键代码中使用的第二种类型
由于您以自己的方式实现了自定义的用户/角色/等等,所以您需要在代码中使用UserManager作为UserManager<ApplicationUser,string>传递给用户PK作为字符串的类型。
发布于 2014-04-07 23:33:39
这对我起了作用。如果您创建自定义用户和角色,则似乎必须创建自己的用户管理器和用户存储。这是派生的UM(您也可以以同样的方式创建rolemanager ):
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()));https://stackoverflow.com/questions/22924816
复制相似问题