1

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>;
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • Can you post the Create function logic. And where exactly are you calling ValidateAsync? – Phael Sep 03 '18 at 12:39
  • Hi! Thanks for answering. The create function seems to be an interface from the identity framework package? And ValidateAsync is called when manager.create is called as well. I added the create interface in the question. – JianYA Sep 03 '18 at 12:50
  • @Phael Hi! Did you manage to find anything? – JianYA Sep 03 '18 at 14:24
  • Sorry mate, could not get back to you until now. I never had an issue implementiting custom fields to IdentityUser or IdentityRole. But I prefer using custom validations as well. I do not have the overview of your entire process from end point to end point to be able to help you as a second eye. I think you should debug through every step to see exactly where it is breaking, maybe it is the connection to the db or the validation logic the problem, who knows. You can test custom validations where you write your own rules and maybe it would work. – Phael Sep 04 '18 at 06:50
  • Hi @Phael. Thanks for your help. My custom validations pass. Its just that it fails when creating. Based on your experience did you ever extend your create function or did the create function have its own validation? – JianYA Sep 04 '18 at 07:01
  • By experience, I had to extend the role collection using mongoDB. I just had to register the Roles collection in my container. If you are using SQL, maybe this will help you https://code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97 or this https://stackoverflow.com/questions/19697226/creating-roles-in-asp-net-identity-mvc-5 – Phael Sep 04 '18 at 13:36
  • Thats alright. I used a separate roles table instead and left the default one alone – JianYA Sep 05 '18 at 01:39
  • Okay cool. I always use my own collection or table. Anyway, if any comment was helpful to you anyhow please give it vote. – Phael Sep 05 '18 at 09:56

0 Answers0