6

Windows ドメインのイントラネット サイト (を使用<authentication mode="Windows" />) で作業しているときに、次の問題に遭遇しました。

[Authorize(Roles = "Domain Users, Domain Admins")]
public class MyController: Controller {...}

Active Directory グループの名前にスペースが含まれているため、このコントローラーはどのユーザーも使用できません。では、スペースを含むロール名 (ここでは AD グループの名前) を使用しているときに、MVC (または ASP.Net) を正しく承認させることはできますか?

応答のない同様の質問:

  1. 役割の承認に使用されるスペースを含む AD グループ
  2. ロールにスペースが含まれている場合の AuthorizeAttribute の記述方法
4

3 に答える 3

6

独自の属性を作成し、AuthorizeAttribute から派生させます。次に、AuthorizeCore メソッドをオーバーライドし、スペースを含むロールの検証を使用して独自のロジックを実装します。

例は次のようになります。

public class CustomAuthAttribute : AuthorizeAttribute
{
   private readonly IUserRoleService _userRoleService;
   private string[] _allowedRoles;

   public CustomAuthAttribute(params string[] roles)
   {
      _userRoleService = new UserRoleService();
      _allowedRoles = roles;
   }
   protected override bool AuthorizeCore(HttpContextBase httpContext)
   {
    //something like this.
    var userName = httpContext.User.Identity.Name;
    var userRoles = _userRoleService .GetUserRoles(userName); // return list of strings
    return _allowedRoles.Any(x => userRoles.Contains(x));
   }

}

使用法

[CustomAuth("role withspace","admin")]
public ActionResult Index()
{
}
于 2012-10-25T06:51:19.250 に答える
3

私がやったとき、スペースの役割はうまくいきます:

public static class AppRoles
{
    public const string Manager     =@"domain\App Manager";
    public const string Admin       =@"domain\App Admin";
}

[Authorize(Roles = AppRoles.Admin)] 
public class MyAbcController : Controller
{
    //code
}
于 2016-01-29T18:06:52.977 に答える