Asp.net mvc 列出用户时,使用角色的Windows身份验证不起作用,角色具有更高的优先级

Asp.net mvc 列出用户时,使用角色的Windows身份验证不起作用,角色具有更高的优先级,asp.net-mvc,asp.net-mvc-5,roles,roleprovider,Asp.net Mvc,Asp.net Mvc 5,Roles,Roleprovider,我在数据库中有“mydomain\myusername”和管理员角色。我已经用不同的配置运行了一些测试。带有“+”的注释具有访问权限,而登录时需要“-”。看起来,如果一个用户被自己授权,那么它就被授予了访问权限。但是,当添加角色时,该角色具有优先权,它甚至不会查看单个用户 如果在指定角色时考虑单个用户或多个用户,如何使其工作?我使用的是自定义的[DefaultAuthorize]:和OverrideAuthorize,这样控制器和操作权限就不会同时存在,但它不会导致用户在角色上被忽略的行为。该行

我在数据库中有“mydomain\myusername”和管理员角色。我已经用不同的配置运行了一些测试。带有“+”的注释具有访问权限,而登录时需要“-”。看起来,如果一个用户被自己授权,那么它就被授予了访问权限。但是,当添加角色时,该角色具有优先权,它甚至不会查看单个用户

如果在指定角色时考虑单个用户或多个用户,如何使其工作?我使用的是自定义的[DefaultAuthorize]:和OverrideAuthorize,这样控制器和操作权限就不会同时存在,但它不会导致用户在角色上被忽略的行为。该行为似乎是身份验证的默认行为

编辑:我刚刚对其进行了更多的测试,上面的解决方案对于在控制器/操作中创建或并不起作用,如果两者都已指定,则仍然需要登录,但用户仅在控制器组中。如果用户在操作组中,则该选项有效

编辑:我唯一能确定的是,在动作中定义角色的工作方式与预期一致。在控制器中添加角色或用户会创建无意义的行为

因此,有两个问题让人困惑1。在控制器和操作中指定角色时,似乎无法摆脱和条件。2.用户因角色而被忽略。

我将MVC5与windows身份验证和角色一起使用

//-[Authorize(Roles = "Publisher,Editor", Users = "mydomain\\myusername")] //this should have worked since myusername is running the test
//-[Authorize(Users = "mydomain\\myusername",Roles = "Publisher,Editor")]  //this should have worked also
//+[Authorize(Users = "mydomain\\myusername", Roles = "Administrator,Publisher,Editor")]  //this works because of Administrator
//+[Authorize(Users="mydomain\\myusername")]
//+[Authorize(Roles = "Administrator")]
//+[Authorize(Roles = "Administrator", Users = "mydomain\\myusername")]
//+[Authorize(Roles = "Administrator,Editor", Users = "mydomain\\myusername")]
//+[Authorize(Roles = "Publisher,Administrator,Editor", Users = "mydomain\\myusername")]
[DefaultAuthorize(Roles = "Publisher,Editor")]
public class PersonEntitiesController : Controller
{

    //default role and override role works as long as it is in a group
    //-[Authorize(Roles = "Administrator")] doesn't work as it's AND with controller
    //when a user is grouped with a Role, the role takes priority
    //doesn't work as myusername is ignored and only looks at Publisher but user is not in gorup
    //-[OverrideAuthorize(Users = "mydomain\\myusername",Roles="Publisher")]  
    //+[OverrideAuthorize(Roles="Administrator")]
    //+[OverrideAuthorize(Users = "mydomain\\myusername")] //works as long as myusername is listed by itself
    //+[OverrideAuthorize(Users = "mydomain\\myusername",Roles="Administrator")] //the group works as long as myusername is in that group
    public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)

这是Authorize属性的默认行为。该属性验证是否通过了以下所有规则:

  • 用户不是空的,它有一个标识并且经过身份验证
  • 如果包含用户名,则标识名将包含在这些名称中
  • 如果包含角色,则标识角色上存在包含的任何角色
  • 检查ASP.NET MVC的AuthorizeAttribute.IsAuthorized代码可以确认:

    protected virtual bool IsAuthorized(HttpActionContext actionContext)
    {
        if (actionContext == null)
        {
          throw Error.ArgumentNull("actionContext");
        }
        IPrincipal user = actionContext.ControllerContext.RequestContext.Principal;
        if (user == null || user.Identity == null || !user.Identity.IsAuthenticated)
        {
          return false;
        }
        if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase))
        {
          return false;
        }
        if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole))
        {
          return false;
        }
        return true;
    }
    

    因此,正如您所怀疑的,此行为作为AND而不是OR起作用。如果您想要有不同的行为,我建议您创建一个自定义授权属性,并在其上添加您自己的逻辑。只需从authorized属性继承,并用自定义逻辑覆盖IsAuthorized方法。

    我已编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。