當你用自己的用戶類擴展,你必須提供身份使用類型所有。
這裏是我的上下文類,請注意,我用字符串作爲鍵:
public class EarlyDetectionContext
: IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim>
當然,你也需要爲角色用戶登陸,userclaim和班級的UserRole中。 這裏是我的:
public class EDClaim : IdentityUserClaim<string>
{
}
public class EDLogin : IdentityUserLogin<string>
{
}
[Table("Roles", Schema = "ED")]
public class Role : IdentityRole<string, RoleUserAssociation>
{
[Required, Column("DisplayName")]
public string DisplayName { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
}
[Table("RoleUserAssociations", Schema = "ED")]
public class RoleUserAssociation : IdentityUserRole<string>
{
//
// Due to Identity 2 the Id needs to of string type
//
[Key]
[Required]
public string ID { get; set; }
//[Required, Column("User_Id")]
//public User User { get; set; }
//[Required, Column("Role_Id")]
//public Role Role { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
}
[Table("Users", Schema = "ED")]
public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim>
{
// This is needed for the new Identity 2 framework
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> 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;
}
[Required, Column("Municipality_Id")]
public Municipality Municipality { get; set; }
public string PasswordSalt { get; set; }
[Required, Column("FirstName")]
public string FirstName { get; set; }
[Required, Column("LastName")]
public string LastName { get; set; }
[Column("Title")]
public string Title { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
public bool Complete { get; set; }
[Required]
public DateTime Activated { get; set; }
}
你需要在每類中指定的密鑰類型,在我的情況的字符串,你的情況很長。 另請注意,我的聲明和登錄類是空的。沒關係,我不必在我的情況下添加任何功能或屬性,但我確實需要指定它們。
並且需要User類中的GenerateUserIdentityAsync方法。
在Startup.Auth.cs文件,你需要添加:
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(EarlyDetectionContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
您還需要OWIN啓動類(可以通過右鍵單擊添加 - 添加 - 新項)和Identity.cs文件。
如果您可以閱讀德語,請看看這blog
它基本上描述了你想要做的事情。 (儘管db-first方法)
另外看他的video(他會說英語很好)。
和part 2也是。
我希望這給你足夠的信息前進:)
我有類似的東西。你是否通過'UserManager'的實例化修正了最後一個錯誤?請參閱我的問題http://stackoverflow.com/questions/31024859/how-do-i-expose-usermanageriapplicationuser-to-my-business-layer-while-hidin – Artyom 2015-06-24 11:00:32