【发布时间】: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