本文介绍了针对ApplicationUser角色的ASP MVC生成引发警告导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ApplicationUser模型:

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.

我的实现有问题吗,还是应该以其他方式完成?我添加了Roles导航属性,以便可以实现HasRole和HasPermission方法.

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.

我的Permission和ApplicationRole模型的实现如下:

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

推荐答案

我对 ASP.NET身份不了解.但是经过一番搜索,我可以给你一个大概的答案. IdentityUser 应该具有适当的 Roles ,该角色继承 IdentityUserRole 而不是 IdentityRole .我认为该模型与IdentityUsers和IdentityRoles有关.因此,您应该做的是创建 ApplicationUserRole 类,该类继承 IdentityUserRole :

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

IdentityRole<string, ApplicationUserRole>继承您的ApplicationRole:

Inherit your ApplicationRole from IdentityRole<string, ApplicationUserRole>:

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

然后在 ApplicationUser 类中使用此类.要使用ApplicationUserRole,您需要从IdentityUser

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

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

最后,将您的 ApplicationUser HasPermission 方法更改为:

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.

这篇关于针对ApplicationUser角色的ASP MVC生成引发警告导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:24