ozzii ozzii - 4 months ago 48
C# Question

ASP MVC Build Throwing Warning For ApplicationUser Roles Navigation Property?

I have the following ApplicationUser Model:

public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }

public virtual ICollection<ApplicationRole> Roles { get; set; }

public bool HasRole(string _role)
{
return Roles.Any(r => r.Name == _role);
}

public bool HasPermission(string _permission)
{
return Roles.Any(r => r.Permissions
.Any(p => p.Name == _permission));
}
}


But when I run a build I get the following warning message:

ApplicationUser.Roles hides inherited member
'IdentityUser<string, IdentityUserLogin,IdentityUserRole, IdentityUserClaim>.Roles.
To make the current member override that implementation, add the overide keyword. Otherwise
add the new keyword.


Is something wrong with my implementation or should it be done differently? I have added the Roles navigation property so that I can implement the HasRole and HasPermission methods.

My Permission and ApplicationRole models are implemented as follows:

public class Permission
{
public byte Id { get; set; }
public string Name { get; set; }

public virtual List<ApplicationRole> Roles { get; set; }
}


public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }

public virtual ICollection<Permission> Permissions { get; set; }

public bool IsPermissionInRole(string _permission)
{
return Permissions.Any(p => p.Name == _permission);
}
}

Answer

I do not have broad knowledge about ASP.NET Identity. But after a little search I can give you rough answer. IdentityUser should have proeprty Roles which inherits IdentityUserRole not IdentityRole. I think this model relates IdentityUsers and IdentityRoles. So, what you should do is create ApplicationUserRole class which inherits IdentityUserRole:

public class ApplicationUserRole : IdentityUserRole
{
    public ApplicationUserRole()
        : base()
    { }

    public virtual ApplicationRole Role { get; set; }
}

Inherit your ApplicationRole from IdentityRole<string, ApplicationUserRole>:

public class ApplicationRole 
    : IdentityRole<string, ApplicationUserRole>
{
}

Then use this class in your ApplicationUser class. To use ApplicationUserRole you need to inherit ApplicationUser from IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim> isntead of IdentityUser

public class ApplicationUser 
    : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    .............
}

Finally, change your HasPermission method of ApplicationUser to something like:

public bool HasPermission(string _permission)
{
    return Roles.Any(r => r.Role.IsPermissionInRole(_permission));
}

I am stating again, this is rough answer. For more information about extending Identity models, please refer to this code project article.

Comments