私はMVCアプリケーション用に4層アーキテクチャを使用しています:ドメイン、データ、サービスおよびMVC。 私のドメイン層では、IdentityUserから継承するEmployeeクラスを作成しました。それはカスタムプロパティと私はMVCのidentitymodesでアプリケーションのユーザークラスから得たGenerateUserIdentityAsyncメソッドが含まれています。カスタムIDユーザー(ASP.NET 5.2EF 6およびID 2)を使用する場合のユーザー役割テーブルの余分な列
public class Employee : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<Employee> 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 String CIN { get; set; }
public String FirstName { get; set; }
私のデータレイヤーでは、私はIdentityDbContextから継承したコンテキストを作成しました。私は私の卒業生に従業員は言及していません。私はまた、私のアプリケーション、特に従業員クラスに関して多くを追加しました。
public class MyCWCContexte : IdentityDbContext<Employee>
{
public MyCWCContexte()
: base("name=CWCDB")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
私がやった最後の変更は、彼らが私のEmployeeクラスと私のコンテキストに一致するので、私はStartUp.Auth.csとIdentityConfigファイルを変更したということです。
public class ApplicationUserManager : UserManager<CWC.Domain.Entities.Employee>
{
public ApplicationUserManager(IUserStore<CWC.Domain.Entities.Employee> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<CWC.Domain.Entities.Employee>(context.Get<MyCWCContexte>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<CWC.Domain.Entities.Employee>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
たとえば、IdentityUserRoleクラスのaspnet自動生成テーブルでは、移行からデータベースを生成するのがうまくいきます。
Screenshot of select * from aspnetuserroles ユーザーIDとRoleIdは、テーブルのPKのであり、EMPLOYEE_IDは、従業員からのFKである:ユーザーを追加しようと、それに役割を追加する場合は、ここで述べたように、私はaspnetuserrolesは3列が含まれていることを見てきました。 UserIdはFkでなければならないと思います。 usermanagerからのAuthorizate注釈とGetRolesはうまくいかないので、実際には問題になります。 UserManager.GetRoles(Employee.Id)は常にnullを返し、私が取得したEmployee.Idはデータベースから得た正しい値を持っています。 私はcompariisonがuseridではなくemployee_idで行われているからだと思います。 あなたはこれについて考えていますか?私はASP.NETの初心者です。前もって感謝します。
あなたはまた、独自の役割のクラスを作成しましたか? –