asp.net-mvc - 我应该在哪里放置可以重定向请求的检查?

标签 asp.net-mvc authorization redirect routes

如果用户的密码已过期,我需要将用户重定向到“更改密码”页面。

我想将此代码放在一个地方,以便任何请求都可以重定向到更改密码页面。

我已经研究过扩展 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/

相关文章:

.htaccess - htaccess重定向不同的url进行重定向

.net - ASP.Net MVC 框架和数据绑定(bind)

c# - MVC 模型绑定(bind) - 表单帖子以数字开头

c# - 从 html 助手中获取未编码的 html

python - 为授权编写 CherryPy 装饰器

authentication - Meteor 帐户获取 SAML 响应(登录后的 token /用户)

redirect - URL 片段和 302 重定向

asp.net-mvc - 如何在单击按钮时将部分 View 呈现为模态弹出窗口?

asp.net-mvc - ASP .NET MVC 中的 Authorize 属性是否用于身份验证和授权?

javascript - 使用 jQuery 重定向