本文介绍了应用程序用户角色导航属性的 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.

My 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 Identity 没有广泛的了解.但经过一番搜索,我可以给你粗略的答案.IdentityUser 应该具有继承 IdentityUserRole 而不是 IdentityRole 的属性 Roles.我认为这个模型将 IdentityUsers 和 IdentityRoles 联系起来.因此,您应该做的是创建继承 IdentityUserRoleApplicationUserRole 类:

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 继承您的 ApplicationRole:

Inherit your ApplicationRole from IdentityRole<string, ApplicationUserRole>:

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

然后在您的 ApplicationUser 类中使用这个类.要使用 ApplicationUserRole,您需要从 IdentityUser 继承 ApplicationUser,而不是 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; }

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

最后,将 ApplicationUserHasPermission 方法更改为:

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.

这篇关于应用程序用户角色导航属性的 ASP MVC 构建抛出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:36