aspnet / Identity

[Archived] ASP.NET Core Identity is the membership system for building ASP.NET Core web applications, including membership, login, and user data. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
1.96k stars 869 forks source link

Adding roles with same names for different applications #1953

Closed stardocs-services closed 5 years ago

stardocs-services commented 5 years ago

I have just extended my IdentityRole like this:

public class AspApplicationRoles : IdentityRole
{
    public AspApplicationRoles() : base() { }
    public AspApplicationRoles(String Name) : base(Name) { }
    [Required]
    public String ApplicationId { get; set; }
    public AspNetApplications Application { get; set; }
}

Then, in order to add the same role names but have a different ApplicationId, I created a custom rolevalidator like this:

public class ApplicationRoleValidator<TRole> : RoleValidator<TRole> where TRole : AspApplicationRoles
    {
        private RoleManager<TRole, string> Manager { get; set; }
        private AspApplicationRoles data = new AspApplicationRoles();
        private ApplicationDbContext dbContext = new ApplicationDbContext();

        public ApplicationRoleValidator(RoleManager<TRole, string> manager) : base(manager)
        {
            Manager = manager;
        }

        public override async Task<IdentityResult> ValidateAsync(TRole Input)
        {
            data = dbContext.AspApplicationRoles.Where(ar => ar.ApplicationId == Input.ApplicationId && ar.Name == Input.Name).SingleOrDefault();
            if (data == null)
            {
                return IdentityResult.Success;
            }
            else
            {
                return IdentityResult.Failed("Role already exists");
            }
        }
    }

I then had to change my applicationRoleManagers like this:

public class ApplicationRoleManager : RoleManager<AspApplicationRoles>
{
    public ApplicationRoleManager(IRoleStore<AspApplicationRoles, string> roleStore)
        : base(roleStore)
    {
        RoleValidator = new ApplicationRoleValidator<AspApplicationRoles>(this);
    }
    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var manager = new ApplicationRoleManager(new ApplicationRoleStore(context.Get<ApplicationDbContext>()));
        return manager;
    }
}

I have also removed the unique key on the database tables 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 }));
    }

However, I still can't have the same role names and I still get EntityValidationError "Role Manager already exists. I have tried to search for a solution but I can't seem to find it or see it.

HaoK commented 5 years ago

This appears to be an issue for pre core identity, please open issues for that in https://github.com/aspnet/AspNetIdentity