如果用户的密码已过期,我需要将用户重定向到“更改密码”页面。
我想将此代码放在一个地方,以便任何请求都可以重定向到更改密码页面。
我已经研究过扩展 AuthorizeAttribute 并覆盖 OnActionExecuting,但两者都不起作用/允许我短路路由逻辑以重定向到密码更改页面。
为了稍微澄清一下,逻辑是:
未经授权的请求:
-> 任意 URL -> AuthorizeAttribute -> Login.aspx -> 密码过期 -> ChangePassword.aspx
授权请求:
-> 任何 URL -> ??????? -> 更改密码.aspx
就是那个 ????我不知道该怎么做的部分。
我想我将继续扩展 AuthorizeAttribute。我会在任何地方使用它 除了 密码更改 Controller 方法。
最佳答案
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);
}
}
这工作正常,只需使用此属性标记每个 Controller ,排除“帐户”一个。这样,具有过期属性的用户无法继续,直到更改密码。
关于asp.net-mvc - 我应该在哪里放置可以重定向请求的检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/837046/