我应该在哪里放置可能重定向请求的支票?
-
10-07-2019 - |
题
如果密码已过期,我需要将用户重定向到“更改密码”页面。
我想将此代码放在一个位置,以便可以将任何请求重定向到更改密码页。
我已经研究过扩展AuthorizeAttribute并覆盖OnActionExecuting,但是没有工作/允许我将路由逻辑短路以重定向到密码更改页面。
稍微澄清一点,逻辑是:
未经授权的请求:
- GT <!>;任何网址 - <!> gt; AuthorizeAttribute - <!> gt; Login.aspx - <!> gt;密码已过期 - <!> gt;为ChangePassword.aspx
授权请求:
- GT <!>;任何网址 - <!> gt; ???????? - <!> gt;为ChangePassword.aspx
它那????部分我不知道该怎么做。
我想我将继续扩展AuthorizeAttribute。除了 密码更改控制器方法之外,我将在 处使用它。
解决方案
public class DenyExpiredPasswordAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
IPrincipal user = filterContext.HttpContext.User;
if(user != null)
{
if (user.Identity.IsAuthenticated)
{
if (CurrentUser.PasswordExpired) // your checking of password expiration
{
filterContext.HttpContext.Response.Redirect("~/Account/ChangePassword?reason=expired");
}
}
}
base.OnAuthorization(filterContext);
}
}
这样可以正常工作,只需使用此属性标记每个控制器,排除<!>“帐户<!>”;一。这样,任何具有过期属性的用户都无法继续,直到更改密码。
其他提示
您可以在global.asax中为PostAuthenticateRequest事件添加事件处理程序。
protected void Application_Start(object sender, EventArgs e) {
this.PostAuthenticateRequest += new EventHandler(Global_PostAuthenticateRequest);
}
void Global_PostAuthenticateRequest(object sender, EventArgs e)
{
if (passwordExpired) {
Context.Response.Redirect("~/ChangePassword.aspx");
}
}
不隶属于 StackOverflow