构造函数不执行,求教!!!
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class myAuthorize : AuthorizeAttribute
{
private readonly bool _authorize;
private bool _isPermissionFail = false;
public myAuthorize() {
string test = "断点";//在这里设置断点,F5调试时,并未进入这一步
if (HttpContext.Current.User.Identity.Name != "")
{
_authorize = true;
}
else
{
_authorize = false;
}
}
public myAuthorize(string permission)
{
string test = "断点";//在这里设置断点,,F5调试时,并未进入这一步
if (HttpContext.Current.User.Identity.Name != "")
{
_authorize = PermissionManager.CheckUserHasPermision(HttpContext.Current.User.Identity.Name, permission);
if (_authorize == false)
{
_isPermissionFail = true;
}
}
else
{
_authorize = false;
}
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return _authorize;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
//F5调试发现,程序直接跳过构造函数,执行到这里了
base.OnAuthorization(filterContext);
if (filterContext.HttpContext.Response.StatusCode == 403)
{
filterContext.Result = new RedirectResult("/Admin/Home/PermissionError");
}
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (_isPermissionFail)
{
filterContext.HttpContext.Response.Redirect("/Admin/Home/PermissionError");
}
else
{
//base.HandleUnauthorizedRequest(filterContext);
filterContext.HttpContext.Response.RedirectToRoute(new {Area="Admin",Controller="Home",Action="Login" });
}
}
}
还望各位大侠不吝赐教!!! 调试 属性类 MVC4 类构造函数不执行 MVC4自定义权限设置
[解决办法]
他应该执行的是AuthorizeAttribute里面的构造方法,然后触发了OnAuthorization
[解决办法]
你是否正确调用了myAuthorize 该类,而不是直接写的AuthorizeAttribute
[myAuthorize]
public ActionResult Index()
{
ViewBag.Message = "欢迎使用 ASP.NET MVC!";
return View();
}
必须是线程安全??这个是什么概念呢~
不知道附加了什么特殊的东西~?