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>; 
8
  • Can you post the Create function logic. And where exactly are you calling ValidateAsync? Commented Sep 3, 2018 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. Commented Sep 3, 2018 at 12:50
  • @Phael Hi! Did you manage to find anything? Commented Sep 3, 2018 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. Commented Sep 4, 2018 at 6: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? Commented Sep 4, 2018 at 7:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.