Asp.net mvc 如何将自定义角色管理器插入authorized属性?

Asp.net mvc 如何将自定义角色管理器插入authorized属性?,asp.net-mvc,asp.net-identity,Asp.net Mvc,Asp.net Identity,我有一个不能更改的旧数据库架构。它有一个带有整数字段的单用户表,用于指定用户级别,其中1为标准用户,5为管理员。我正在编写一个MVC前端,我想使用ASP.NET标识。我已经从研究和样板代码中找出了其他的一切。我似乎不知道如何创建自定义角色系统。我意识到它与实现角色管理器和角色存储有关。这很好,但是我如何将其与MVC连接,以获得authorized属性来确认我的经理 我很抱歉,如果这是显而易见的,但我已经做了我的研究,我有困难确定下来 根据您的问题,我假设您已经了解了如何创建您的角色管理器,而您只

我有一个不能更改的旧数据库架构。它有一个带有整数字段的单用户表,用于指定用户级别,其中1为标准用户,5为管理员。我正在编写一个MVC前端,我想使用ASP.NET标识。我已经从研究和样板代码中找出了其他的一切。我似乎不知道如何创建自定义角色系统。我意识到它与实现角色管理器和角色存储有关。这很好,但是我如何将其与MVC连接,以获得
authorized属性
来确认我的经理


我很抱歉,如果这是显而易见的,但我已经做了我的研究,我有困难确定下来

根据您的问题,我假设您已经了解了如何创建您的角色管理器,而您只是缺少实际使用它的配置。如果我的假设是错误的,请告诉我,我将添加有关如何创建CustomRoleManager的说明

Web.config

<configuration>
  <system.web>
    <roleManager enabled="true" defaultProvider="CustomRoleProvider">
      <providers>
        <clear/>
        <add name="CustomRoleProvider"
             type="MyNamespace.CustomRoleProvider,
                   MyNamespace, Version=1.0.0.0, Culture=neutral"
             connectionStringName="MyConnectionString"
             enablePasswordRetrieval="false"
             enablePasswordReset="false"
             requiresQuestionAndAnswer="false"
             writeExceptionsToEventLog="false" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

根据您的问题,我假设您已经了解了如何创建您的角色管理器,而您只缺少实际使用它的配置。如果我的假设是错误的,请告诉我,我将添加有关如何创建CustomRoleManager的说明

Web.config

<configuration>
  <system.web>
    <roleManager enabled="true" defaultProvider="CustomRoleProvider">
      <providers>
        <clear/>
        <add name="CustomRoleProvider"
             type="MyNamespace.CustomRoleProvider,
                   MyNamespace, Version=1.0.0.0, Culture=neutral"
             connectionStringName="MyConnectionString"
             enablePasswordRetrieval="false"
             enablePasswordReset="false"
             requiresQuestionAndAnswer="false"
             writeExceptionsToEventLog="false" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

如果有人有相同的琐碎需求,这里是我使用的
角色提供者。如果您知道此实现不安全的任何原因,请告诉我。我在Web.Config中使用@Pluc的答案将此提供程序连接到我的应用程序。它工作得非常好

public class AppRole : IRole<int>
{
    public AppRole(int a_id, string a_name)
    {
        Id = a_id;
        Name = a_name;
    }

    public int Id { get; private set; }
    public string Name { get; set; }
}

public class AppRoleProvider : RoleProvider
{
    private readonly IServiceLocator _container = UnityConfig.GetServiceLocator();
    private ITrainingRepository _repository; // Thin wrapper around my DbContext

    private AppRole[] _roles = new[]
        {
            new AppRole(0, "User"),
            new AppRole(5, "Admin"),
        };

    public AppRoleProvider()
    {
        ApplicationName = "TrainingCenter";

        _repository = _container.GetInstance<ITrainingRepository>();
    }

    public override string ApplicationName { get; set; }

    public override bool IsUserInRole(string username, string roleName)
    {
        var user = _repository.GetUserByUserName(username);
        if (user == null)
            return false;

        var role = _roles.FirstOrDefault(i => i.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase));

        if (role == null)
            return false;

        if (user.UserLevel >= role.Id)
            return true;

        return false;
    }

    public override string[] GetRolesForUser(string username)
    {
        var user = _repository.GetUserByUserName(username);
        if (user == null)
            return new string[] {};

        return _roles.Where(i => i.Id <= user.UserLevel).Select(i => i.Name).ToArray();
    }

    public override void CreateRole(string roleName)
    {
        // Does not create.
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        // Does not delete.
        return false;
    }

    public override bool RoleExists(string roleName)
    {
        return _roles.Any(i => i.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase));
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        // Does not add user to role.
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        // Does not remove users from roles.
    }

    public override string[] GetUsersInRole(string roleName)
    {
        // Does not get users in role.
        return new string[] {};
    }

    public override string[] GetAllRoles()
    {
        return _roles.Select(i => i.Name).ToArray();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        // Does not find users in role.
        return new string[] { };
    }

}
公共类许可证:IRole
{
公共许可(int a_id,string a_name)
{
Id=a_Id;
名称=一个名称;
}
public int Id{get;private set;}
公共字符串名称{get;set;}
}
公共类批准提供程序:角色提供程序
{
私有只读IServiceLocator_container=UnityConfig.GetServiceLocator();
私有ITrainingRepository _repository;//我的DbContext的精简包装器
私人许可[]_角色=新[]
{
新批准(0,“用户”),
新批准(5,“管理”),
};
公共批准提供人()
{
ApplicationName=“培训中心”;
_repository=_container.GetInstance();
}
公共重写字符串ApplicationName{get;set;}
public override bool IsUserInRole(字符串用户名、字符串角色名)
{
var user=\u repository.GetUserByUserName(用户名);
if(user==null)
返回false;
var role=_roles.FirstOrDefault(i=>i.Name.Equals(roleName,StringComparison.OrdinalIgnoreCase));
如果(角色==null)
返回false;
if(user.UserLevel>=role.Id)
返回true;
返回false;
}
公共重写字符串[]GetRolesForUser(字符串用户名)
{
var user=\u repository.GetUserByUserName(用户名);
if(user==null)
返回新字符串[]{};
返回_roles.Where(i=>i.Id i.Name).ToArray();
}
公共重写无效CreateRole(字符串roleName)
{
//不创建。
}
public override bool DeleteRole(字符串roleName,bool throwOnPopulatedRole)
{
//不删除。
返回false;
}
公共覆盖布尔RoleExists(字符串roleName)
{
返回_roles.Any(i=>i.Name.Equals(roleName,StringComparison.OrdinalIgnoreCase));
}
public override void AddUsersToRoles(字符串[]用户名,字符串[]角色名)
{
//不将用户添加到角色。
}
public override void RemoveUsersFromRoles(字符串[]用户名,字符串[]角色名)
{
//不从角色中删除用户。
}
公共重写字符串[]GetUsersInRole(字符串roleName)
{
//不获取角色中的用户。
返回新字符串[]{};
}
公共重写字符串[]GetAllRoles()
{
return_roles.Select(i=>i.Name.ToArray();
}
公共重写字符串[]FindUsersInRole(字符串roleName,字符串usernameToMatch)
{
//找不到角色中的用户。
返回新字符串[]{};
}
}

如果有人有相同的琐碎需求,这里是我使用的
角色提供者。如果您知道此实现不安全的任何原因,请告诉我。我在Web.Config中使用@Pluc的答案将此提供程序连接到我的应用程序。它工作得非常好

public class AppRole : IRole<int>
{
    public AppRole(int a_id, string a_name)
    {
        Id = a_id;
        Name = a_name;
    }

    public int Id { get; private set; }
    public string Name { get; set; }
}

public class AppRoleProvider : RoleProvider
{
    private readonly IServiceLocator _container = UnityConfig.GetServiceLocator();
    private ITrainingRepository _repository; // Thin wrapper around my DbContext

    private AppRole[] _roles = new[]
        {
            new AppRole(0, "User"),
            new AppRole(5, "Admin"),
        };

    public AppRoleProvider()
    {
        ApplicationName = "TrainingCenter";

        _repository = _container.GetInstance<ITrainingRepository>();
    }

    public override string ApplicationName { get; set; }

    public override bool IsUserInRole(string username, string roleName)
    {
        var user = _repository.GetUserByUserName(username);
        if (user == null)
            return false;

        var role = _roles.FirstOrDefault(i => i.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase));

        if (role == null)
            return false;

        if (user.UserLevel >= role.Id)
            return true;

        return false;
    }

    public override string[] GetRolesForUser(string username)
    {
        var user = _repository.GetUserByUserName(username);
        if (user == null)
            return new string[] {};

        return _roles.Where(i => i.Id <= user.UserLevel).Select(i => i.Name).ToArray();
    }

    public override void CreateRole(string roleName)
    {
        // Does not create.
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        // Does not delete.
        return false;
    }

    public override bool RoleExists(string roleName)
    {
        return _roles.Any(i => i.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase));
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        // Does not add user to role.
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        // Does not remove users from roles.
    }

    public override string[] GetUsersInRole(string roleName)
    {
        // Does not get users in role.
        return new string[] {};
    }

    public override string[] GetAllRoles()
    {
        return _roles.Select(i => i.Name).ToArray();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        // Does not find users in role.
        return new string[] { };
    }

}
公共类许可证:IRole
{
公共许可(int a_id,string a_name)
{
Id=a_Id;
名称=一个名称;
}
public int Id{get;private set;}
公共字符串名称{get;set;}
}
公共类批准提供程序:角色提供程序
{
私有只读IServiceLocator_container=UnityConfig.GetServiceLocator();
私有ITrainingRepository _repository;//我的DbContext的精简包装器
私人许可[]_角色=新[]
{
新批准(0,“用户”),
新批准(5,“管理”),
};
公共批准提供人()
{
ApplicationName=“培训中心”;
_repository=_container.GetInstance();
}
公共重写字符串ApplicationName{get;set;}
public override bool IsUserInRole(字符串用户名、字符串角色名)
{
var user=\u repository.GetUserByUserName(用户名);
if(user==null)
返回false;
var role=_roles.FirstOrDefault(i=>i.Name.Equals(roleName,StringComparison.OrdinalIgnoreCase));
如果(角色==null)
返回false;
if(user.UserLevel>=role.Id)
返回true;
返回false;
}
公共重写字符串[]GetRolesForUser(字符串用户名)
{
var user=\u repository.GetUserByUserName(用户名);
if(user==null)
返回新字符串[]{};
返回_roles.Where(i=>i.Id i.Name).ToArr