Hes*_*sky 6 authorization roles asp.net-mvc-3
我正在创建自己的自定义authorize属性,覆盖AuthorizeCore方法,并想知道是否可以访问已传递到authorize属性标记的角色.
所以,例如,如果我有这个:
[CustomAuthorize(Roles = "Administrator, Sales, Entry")]
Run Code Online (Sandbox Code Playgroud)
是否可以从这里访问这些:
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
}
Run Code Online (Sandbox Code Playgroud)
然后我可以拆分字符串并创建一个数组.
你可以这this.Roles是一个你需要拆分的字符串.
源代码是免费提供的.
默认的AuthorizeCore实现:
protected virtual bool AuthorizeCore(HttpContextBase httpContext) {
if (httpContext == null) {
throw new ArgumentNullException("httpContext");
}
IPrincipal user = httpContext.User;
if (!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;
}
Run Code Online (Sandbox Code Playgroud)
它们有一个内部拆分功能,如下所示:
internal static string[] SplitString(string original) {
if (String.IsNullOrEmpty(original)) {
return new string[0];
}
var split = from piece in original.Split(',')
let trimmed = piece.Trim()
where !String.IsNullOrEmpty(trimmed)
select trimmed;
return split.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4819 次 |
| 最近记录: |