具有声明的自定义角色提供者

Custom role provider with Claims

我的数据库中有用户 table,我在其中保留用户角色(主管理员、管理员、开发人员)。我想授权一些控制器 所以只有主管理员可以访问。

namespace TicketSystem.Controllers
{
    public class UserCredentials : ClaimsPrincipal, IIdentity, IPrincipal
    {
         public IIdentity Identity { get; private set; }
         public int UserId { get; set; }
         public string FirstName { get; set; }
         public string LastName { get; set; }
         public string[] roles { get; set; }

         public string email { get; set; }

         override
         public bool IsInRole(string role)
         {
             if (roles.Any(r => role.Contains(r)))
             {
                 return true;
             }
             else
             {
                 return false;
             }
         }

         public UserCredentials() { }
         public UserCredentials(ClaimsPrincipal principal)
             : base(principal)
         {
         }

         public UserCredentials(int userId, string email, string firstName, string lastName, string[] roles)
         {

             this.Identity = new GenericIdentity(email);
             this.UserId = userId;

             this.email = email;
             this.FirstName = firstName;
             this.LastName = lastName;
             this.roles = roles;

         }


         override
         public string ToString()
         {
             return UserId + "";
         }

    }
}

这是我的登录方式

UserCredentials loggedUser = null;
User loginUser = db.tblUser.Where(x => x.email == model.UserName).FirstOrDefault();
loggedUser = new UserCredentials( loginUser.idUser, 
                 loginUser.email, loginUser.firsName, loginUser.lastName, new string[] { loginUser.role });
if (loggedUser != null)
{
    var identity = new ClaimsIdentity(new[] { 
                    new Claim(ClaimTypes.Name, loggedUser.email),
                    new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", User.Identity.AuthenticationType),
                    new Claim(ClaimTypes.NameIdentifier, loggedUser.FirstName),
                    new Claim(ClaimTypes.Role, loggedUser.roles[0])
                    }, "ApplicationCookie");

    var ctx = Request.GetOwinContext();
    var authManager = ctx.Authentication;

    authManager.SignIn(identity);

我试试这个

public class CustomRoleProvider : RoleProvider
{
    public override bool IsUserInRole(string username, string roleName)
    {
        using (var usersContext = new TicketSystemEntities())
        {
            var user = usersContext.tblUser.SingleOrDefault(u => u.email == username);
            if (user == null)
                return false;
            return user.role != null && user.role==roleName;
        }
    }
}

但我不知道如何配置web.Config。我也有错误,例如

TicketSystem.Models.CustomRoleProvider' does not implement inherited abstract member 'System.Web.Security.RoleProvider.GetUsersInRole(string)

我正在搜索其他示例,但没有找到作者使用 Claim

的任何示例

RoleProvider是一个抽象class,你必须实现所有的抽象方法来编译你的CustomRoleProvider

Web.config 中,您需要添加部分 roleManager 并添加您的自定义提供程序。像这样:

<roleManager enabled="true" defaultProvider="CustomRoleProvider">
  <providers>
    <clear/>
    <add name="CustomRoleProvider" 
       type="TicketSystem.Models.CustomRoleProvider, 
             TicketSystem, Version=1.0.0.0, Culture=neutral" 
       connectionStringName="TicketSystemEntities"
       enablePasswordRetrieval="false" enablePasswordReset="true"/>
  </providers>
</roleManager>

查看 RoleProvider 文档作为参考 https://msdn.microsoft.com/en-us/library/system.web.security.roleprovider(v=vs.140).aspx and roleManager docs https://msdn.microsoft.com/en-us/library/vstudio/ms164660%28v=vs.100%29.aspx