![](/img/trans.png)
[英]DataAnnotation attributes buddy class strangeness - ASP.NET MVC
[英]How to redirect to a view from a buddy class asp.net MVC?
这是我的authorize类,它覆盖默认的AurthorizeCore,如果用户未经授权,我想将其重定向到错误页面。 我该怎么做?
public class UserAcess : AuthorizeAttribute
{
private UserRepository _userRepo = new UserRepository();
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (isAuthorized)
{
var canUse = this._userRepo.CanUserUseApp(httpContext.User.Identity.Name);
// If you can't use this app, guess what? ERROR PAGE fun times.
if (!canUse)
{
isAuthorized = false;
//redirect the user a view that I've made here.
return isAuthorized;
}
}
var personRole = this._userRepo.getPersonRolebyAdName(httpContext.User.Identity.Name);
//TODO Refactor this so that it checks if it's filled.
httpContext.Session["PersonID"] = personRole.Person.PersonID;
httpContext.Session["PersonRoleID"] = personRole.PersonRoleID;
httpContext.Session["UserName"] = personRole.Person.UserName;
httpContext.Session["Role"] = personRole.Role.Description;
httpContext.Session["FirstName"] = personRole.Person.FirstName;
httpContext.Session["LastName"] = personRole.Person.LastName;
return isAuthorized;
}
}
您可以通过覆盖HandleUnauthorizedRequest方法来做到这一点:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new ViewResult
{
ViewName = "SomeUnauthorizedViewName"
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.