gpt4 book ai didi

asp.net-mvc-4 - 覆盖 MVC4 应用程序的 User.IsInRole 和 [Authorize(Roles = "Admin")]

转载 作者:行者123 更新时间:2023-12-01 09:00:15 26 4
gpt4 key购买 nike

我为我的 MVC4 应用程序创建了一个自定义角色提供程序,我已经成功地覆盖了 CreateRole、GetAllRoles 和 RoleExists 方法并将它们链接到我现有的数据库,如下所示:

namespace Project.Providers
{
public class MyProvider : System.Web.Security.SqlRoleProvider
{
private MyContext dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
private Repository<MyUser> userRepository;
private Repository<Role> roleRepository;

public MyProvider()
{
this.userRepository = new Repository<MyUser>(dbcontext);
this.roleRepository = new Repository<Role>(dbcontext);
}

public override string[] GetAllRoles()
{
IEnumerable<Role> dbRoles = roleRepository.GetAll();
int dbRolesCount = roleRepository.GetAll().Count();
string[] roles = new string[dbRolesCount];
int i = 0;
foreach(var role in dbRoles)
{
roles[i] = role.Name;
i++;
}
return roles;
}

public override bool RoleExists(string roleName)
{
string[] roles = { "Admin", "User", "Business" };
if(roles.Contains(roleName))
return true;
else
return false;
}

public override void CreateRole(string roleName)
{
Role newRole = new Role();
newRole.Name = roleName;
roleRepository.Add(newRole);
roleRepository.SaveChanges();
}

public override bool IsUserInRole(string userName, string roleName)
{
MyUser user = userRepository.Get(u => u.Username == userName).FirstOrDefault();
Role role = roleRepository.Get(r => r.Name == roleName).FirstOrDefault();
if (user.RoleID == role.RoleID)
return true;
else
return false;
}
}
}

我一直无法找到一种方法来覆盖

User.IsInRole(string roleName)

当我使用时,我还必须做什么:

[Authorize(Roles = "Admin")]

它将基于我设置的角色提供程序,而不是 asp 默认值。

我的用户类现在如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Collections;
using System.Security.Principal;

namespace Project.Data
{
public class MyUser : IPrincipal
{
[Key]
public int UserID { get; set; }

[StringLength(128)]
public string Username { get; set; }

.....other properties

public IIdentity Identity { get; set; }

public bool IsInRole(string role)
{
if (this.Role.Name == role)
{
return true;
}
return false;
}

public IIdentity Identity
{
get { throw new NotImplementedException(); }
}
}
}

我的堆栈跟踪似乎在以下位置:

System.Web.Security.RolePrincipal.IsInRole(String role) 

所以我尝试以与设置自定义 Provider 相同的方式实现自定义 RolePrincipal 有什么想法可以做到这一点吗?不确定它需要什么构造函数参数。这是我的尝试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration.Provider;
using Project.Data;
using System.Web.Security;
using System.Security.Principal.IIdentity;

namespace Project.Principal
{
public class MyPrincipal : System.Web.Security.RolePrincipal
{
private MyContext dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
private Repository<MyUser> userRepository;
private Repository<Role> roleRepository;

public MyPrincipal()
{
this.userRepository = new Repository<MyUser>(dbcontext);
this.roleRepository = new Repository<Role>(dbcontext);
}

public override bool IsInRole(string role)
{
//code to be added
return true;
}
}

}

最佳答案

您只需要覆盖自定义角色提供程序中的 GetRolesForUser 方法,而不是更符合逻辑的 IsUserInRole,因为这是默认实现调用的方法,会执行一些不需要的缓存。

关于asp.net-mvc-4 - 覆盖 MVC4 应用程序的 User.IsInRole 和 [Authorize(Roles = "Admin")],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com