Duplicate Role Names on Asp.Net Identity and Multi-tenancy

前端 未结 1 1469
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 06:25

I\'m developing a Multi-tenancy web application with ASP.Net MVC and Identity 2.0. I have extended the IdentityRole like this:

public class ApplicationRole :         


        
相关标签:
1条回答
  • 2021-01-13 07:03

    Simply alter database's schema on ApplicationDbContext's OnModelCreating method like this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        var role = modelBuilder.Entity<IdentityRole>()
            .ToTable("AspNetRoles");
        role.Property(r => r.Name)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnAnnotation("Index", new IndexAnnotation(
                new IndexAttribute("RoleNameIndex") 
                { IsUnique = false }));
    }
    

    But you must customize RoleValidator class also. Because default role validator invalidates duplicated role names.

    public class MyRoleValidator:RoleValidator<ApplicationRole>
    {
         public override async Task<IdentityResult> ValidateAsync(ApplicationRole item)
         {
             // implement your validation logic here
    
             return IdentityResult.Success;
         }
    }
    

    Now every time you create the role manager you must set the role validator.

    roleManager.RoleValidator=new MyRoleValidator();
    
    0 讨论(0)
提交回复
热议问题