独自の属性を作成し、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()
{
}