为不同的应用程序添加具有相同名称的角色

时间:2018-09-03 04:29:26

标签: c# asp.net asp.net-mvc entity-framework

我刚才这样扩展了IdentityRole:

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

然后,为了添加相同的角色名称但具有不同的ApplicationId,我创建了一个自定义角色验证器,如下所示:

    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");
            }
        }
    }

然后我必须像这样更改applicationRoleManagers:

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;
    }
}

我还删除了数据库表上的唯一键,如下所示:

    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 }));
    }

但是,我仍然没有相同的角色名称,并且仍然得到EntityValidationError“角色管理器已经存在。我试图搜索解决方案,但似乎找不到或看不到它。

编辑:

这就是我称为新角色验证器的方式

    public bool Save(SaveRolesDetailsViewModel Input)
    {
        bool result = false;
        AspApplicationRoles data = new AspApplicationRoles();
        var store = new ApplicationRoleStore(new ApplicationDbContext());
        var manager =new ApplicationRoleManager(store);
        try
        {
            var role = new AspApplicationRoles
            {
                Name = Input.RoleName,
                ApplicationId = Input.ApplicationId
            };
            var res = manager.RoleValidator.ValidateAsync(role);
            if (res.Result.Succeeded)
            {
                manager.Create(role);
                result = true;
            }
            else
            {
                result = false;
            }
        }
        catch (Exception e)
        {
            logger.Error(e, AspNetEventLogs.NotFound);
        }
        return result;
    }

编辑2:

添加了在添加OnModelCreating之后生成的迁移文件:

    public override void Up()
    {
        DropIndex("dbo.AspNetRoles", "RoleNameIndex");
        CreateIndex("dbo.AspNetRoles", "Name", name: "RoleNameIndex");
    }

    public override void Down()
    {
        DropIndex("dbo.AspNetRoles", "RoleNameIndex");
        CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex");
    }

0 个答案:

没有答案