针对ApplicationUser Roles导航属性的ASP MVC Build Throwing Warning?
我有以下ApplicationUser模型:
public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection 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)); } }
但是当我运行构建时,我收到以下警告消息:
ApplicationUser.Roles hides inherited member 'IdentityUser.Roles. To make the current member override that implementation, add the overide keyword. Otherwise add the new keyword.
我的实现有问题还是应该以不同的方式完成? 我添加了Roles导航属性,以便我可以实现HasRole和HasPermission方法。
我的Permission和ApplicationRole模型实现如下:
public class Permission { public byte Id { get; set; } public string Name { get; set; } public virtual List Roles { get; set; } } public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public virtual ICollection Permissions { get; set; } public bool IsPermissionInRole(string _permission) { return Permissions.Any(p => p.Name == _permission); } }
我对ASP.NET身份没有广泛的了解。 但经过一番搜索,我可以给你粗略的回答。 IdentityUser应该具有inheritanceIdentityUserRole而非IdentityRole的 proeprty 角色 。 我认为这个模型与IdentityUsers和IdentityRoles有关。 那么,你应该做的是创建inheritanceIdentityUserRole的 ApplicationUserRole类:
public class ApplicationUserRole : IdentityUserRole { public ApplicationUserRole() : base() { } public virtual ApplicationRole Role { get; set; } }
从IdentityRole
inheritance您的ApplicationRole IdentityRole
:
public class ApplicationRole : IdentityRole { }
然后在ApplicationUser类中使用此类。 要使用ApplicationUserRole,您需要从IdentityUser
inheritanceApplicationUser IdentityUser
不是IdentityUser
public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } ............. }
最后,将ApplicationUser的HasPermission方法更改为:
public bool HasPermission(string _permission) { return Roles.Any(r => r.Role.IsPermissionInRole(_permission)); }
我再说一遍,这是粗略的回答。 有关扩展Identity模型的更多信息,请参阅此代码项目文章 。
- entity framework6和Asp.Net Identity UserManager:多个DbContext
- 无法从AspNetUsers表中检索inheritance的自定义属性值
- 用户(IPrincipal)在ApiController的构造函数上不可用,使用Web Api 2.1和Owin
- 在ASP.Net Identity 2.0上包含属性UserManager.Users.ToListAsync和UserManager.FindByIdAsync
- 多站点ASP.NET身份2的中间件
- 微服务架构中的ASP.NET身份
- 尝试在ASP.NET Identity 2.0中更改表名
- 用户通过“记住我”退出
- ASP身份2 + Web API令牌身份validation – 持久声明未加载