16 asp.net-mvc authorize-attribute
我的问题是AuthorizeCore方法是如何工作的?
例如,当我想创建自定义Authorize属性时,我发现很多程序员都使用这个代码
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后他们编写自己的代码.
那么这段代码扮演的角色是什么,并且该方法仅检查管理员和计算机管理中的其他创建用户等Windows用户,如果我们自定义它以在表单身份验证中使用.
我也发现了这段代码,但我不明白为什么开发人员将用户存储在cookie和会话中而不是会话中.
在PHP中,我曾经只在会话中存储用户,并检查他是否存在于会话中.
les*_*ess 12
它是开源的,代码可以在这里找到:
https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/AuthorizeAttribute.cs
而这里的具体方法是:
// This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method.
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)
希望有所帮助.
归档时间: |
|
查看次数: |
9742 次 |
最近记录: |