I have recently extended my AspNetRoles model to include an extra field called ApplicationId. The intention is so that I can use the same role names but have different ApplicationId.
public class AspApplicationRoles : IdentityRole
{
public AspApplicationRoles() : base() { }
public AspApplicationRoles(String Name) : base(Name) { }
[Required]
public String ApplicationId { get; set; }
public AspNetApplications Application { get; set; }
}
I also added an OnModelCreating function in my IdentityConfig area that disabled the unique key on the Name field.
I then also updated the validator 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");
}
}
}
}
However, when I debug and run this line: var res = manager.Create(role);
It returns res.Succeeded but does not add the role name to my table.
Is there a way to extend or change the create function?
EDIT:
This is the Create part from Identity
//
// Summary:
// Create a role
//
// Parameters:
// manager:
//
// role:
public static IdentityResult Create<TRole, TKey>(this RoleManager<TRole, TKey> manager, TRole role)
where TRole : class, IRole<TKey>
where TKey : IEquatable<TKey>;