hor*_*rgh 4 c# asp.net authorization asp.net-mvc-3
在Windows域内联网站点上工作时,<authentication mode="Windows" />我遇到了以下问题:
[Authorize(Roles = "Domain Users, Domain Admins")]
public class MyController: Controller {...}
Run Code Online (Sandbox Code Playgroud)
由于活动目录组的名称中有空格,因此该控制器不适用于任何用户.因此,在使用带空格的角色名称(此处:AD组的名称)时,是否可以正确授权MVC(或ASP.Net)?
只是类似的问题,没有回应:
创建自己的属性并从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));
}
Run Code Online (Sandbox Code Playgroud)
}
用法
[CustomAuth("role withspace","admin")]
public ActionResult Index()
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2679 次 |
| 最近记录: |