ASP.NET 会员密码过期

发布于 2024-07-10 03:03:33 字数 304 浏览 8 评论 0原文

我正在使用 ASP.NET 成员资格来验证我的 Web 应用程序。 这对我来说非常有用。 我现在必须实现密码过期。

如果密码已过期,则应将用户重定向到 ChangePassword 屏幕,并且在不更改密码的情况下不应允许用户访问应用程序的任何其他部分。

有很多aspx页面。 如果密码已过期,一种解决方案可能是重定向到每个 aspx 的 ChangePassword 屏幕 OnInit。 有没有其他的解决办法或者建议。

谢谢, 贾伊

I am using ASP.NET membership for the authentication of my web app. This worked great for me. I now have to implement password expiration.

If the password has expired the user should be redirected to ChangePassword screen and should not be allowed access to any other part of the application without changing the password.

There are many aspx pages. One solution could be to redirect to the ChangePassword screen OnInit of every aspx if the password has expired. Is there any other solutions or recommendations.

Thanks,
Jai

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

南风几经秋 2024-07-17 03:03:33

除了csgero的回答,我发现你不需要在 ASP.Net 2.0 (3.5) 中显式为此事件添加事件处理程序。

您只需在 global.asax 中创建以下方法,它就会为您连接起来:

void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    if (this.User.Identity.IsAuthenticated)
    {
        // get user
        MembershipUser user = Membership.GetUser();

        // has their password expired?
        if (user != null
            && user.LastPasswordChangedDate.Date.AddDays(90) < DateTime.Now.Date
            && !Request.Path.EndsWith("/Account/ChangePassword.aspx"))
        {
            Server.Transfer("~/ChangePassword.aspx");
        }
    }
}

Further to csgero's answer, I found that you don't need to explicitly add an event handler for this event in ASP.Net 2.0 (3.5).

You can simply create the following method in global.asax and it gets wired up for you:

void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    if (this.User.Identity.IsAuthenticated)
    {
        // get user
        MembershipUser user = Membership.GetUser();

        // has their password expired?
        if (user != null
            && user.LastPasswordChangedDate.Date.AddDays(90) < DateTime.Now.Date
            && !Request.Path.EndsWith("/Account/ChangePassword.aspx"))
        {
            Server.Transfer("~/ChangePassword.aspx");
        }
    }
}
停滞 2024-07-17 03:03:33

您可以在 global.asax 中为 HttpApplication.PostAuthenticateRequest 事件添加事件处理程序并在那里处理重定向。

You could add an event handler for the HttpApplication.PostAuthenticateRequest event in global.asax and handle the redirection there.

风和你 2024-07-17 03:03:33

根据 Andrew 的回答,我发现您需要检查用户是否已经在更改密码页面上,否则他们会永远无法真正更改其密码,因此永远不要离开更改密码站点:

void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (this.User.Identity.IsAuthenticated)
        {
            // get user 
            MembershipUser user = Membership.GetUser();

            // has their password expired? 
            if (user != null
                && user.LastPasswordChangedDate.AddMinutes(30) < DateTime.Now
                && !Request.Path.EndsWith("/Account/ChangePassword.aspx"))
            {
                Server.Transfer("~/Account/ChangePassword.aspx");
            }
        }
    } 

Further to Andrew's answer, I found you need to check that the user is not already on the change password page, or they will never be able to actually change their password, and hence never leave the change password site:

void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (this.User.Identity.IsAuthenticated)
        {
            // get user 
            MembershipUser user = Membership.GetUser();

            // has their password expired? 
            if (user != null
                && user.LastPasswordChangedDate.AddMinutes(30) < DateTime.Now
                && !Request.Path.EndsWith("/Account/ChangePassword.aspx"))
            {
                Server.Transfer("~/Account/ChangePassword.aspx");
            }
        }
    } 
ヅ她的身影、若隐若现 2024-07-17 03:03:33

只需大约一个小时即可完成此操作,无需修改您的基本页面。 您需要执行的操作如下:

  1. 响应会员控件的 LoggingIn 事件

  2. Find在会员数据库中查找用户并获取 LastPasswordChangedDate

  3. 使用 TimeSpan,将其与当前日期进行比较并确定上次更改密码的时间是否超过了所需的天数。 我从 web.config 获取此值

  4. 如果过期,重定向到 ChangePassword 屏幕

Just implemented this in about an hour, no need to modify your base page. Heres what you have to do:

  1. Respond to the LoggingIn event of the membership control

  2. Find the user in the membership database and get LastPasswordChangedDate

  3. Using a TimeSpan, compare this with the current date and decide if the password was last changed more than the requisite number of days ago. I get this value from web.config

  4. If expired, redirect to the ChangePassword screen

苏别ゝ 2024-07-17 03:03:33

我来这里寻找解决方案,但我当前的技术是 ASP.NET MVC。 因此,为了帮助其他人:您可以扩展 AuthorizeAttribute,并重写 OnAuthorization 方法,如下所示:

public class ExpiredPasswordAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        IPrincipal user = filterContext.HttpContext.User;

        if(user != null && user.Identity.IsAuthenticated)
        {
            MembershipUser membershipUser = Membership.GetUser();

            if (PasswordExpired) // Your logic to check if password is expired...
            {
                filterContext.HttpContext.Response.Redirect(
                    string.Format("~/{0}/{1}?{2}", MVC.SGAccount.Name, MVC.SGAccount.ActionNames.ChangePassword,
                    "reason=expired"));

            }
        }

        base.OnAuthorization(filterContext);
    }
}

注意:我使用 T4MVC 来检索上面代码中的控制器和操作名称。

使用此属性标记除“AccountController”之外的所有控制器。 这样做,密码过期的用户将无法浏览该网站。

这是我就该主题发表的一篇文章,其中有一些优点:

ASP.NET MVC 中的用户密码过期过滤器属性

I got here looking for a solution to this but my current technology is ASP.NET MVC. So to help others: you can extend the AuthorizeAttribute, and override OnAuthorization method, like this:

public class ExpiredPasswordAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        IPrincipal user = filterContext.HttpContext.User;

        if(user != null && user.Identity.IsAuthenticated)
        {
            MembershipUser membershipUser = Membership.GetUser();

            if (PasswordExpired) // Your logic to check if password is expired...
            {
                filterContext.HttpContext.Response.Redirect(
                    string.Format("~/{0}/{1}?{2}", MVC.SGAccount.Name, MVC.SGAccount.ActionNames.ChangePassword,
                    "reason=expired"));

            }
        }

        base.OnAuthorization(filterContext);
    }
}

Note: I use T4MVC to retrieve the Controller and Action names in the code above.

Mark all controllers with this attribute except "AccountController". Doing so no user with an expired password will be able to surf the site.

Here's a post I did on the subject with some bonus points:

User Password Expired filter attribute in ASP.NET MVC

爱你是孤单的心事 2024-07-17 03:03:33

我使用了上面的代码,只对其进行了轻微修改,以便使用 .NET 身份提供程序在 Asp.NET (4.5) MVC5 中实现。 把它留在这里给下一个人/女孩:)

void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (this.User.Identity.IsAuthenticated)
        {
            WisewomanDBContext db = new WisewomanDBContext();

            // get user
            var userId = User.Identity.GetUserId();
            ApplicationUser user = db.Users.Find(userId);

            // has their password expired?
            if (user != null && user.PasswordExpires <= DateTime.Now.Date
                && !Request.Path.EndsWith("/Manage/ChangePassword"))
            {
                Response.Redirect("~/Manage/ChangePassword");
            }

            db.Dispose();
        }
    }

I used the code from above and only slightly modified it to implement in Asp.NET (4.5) MVC5 using the .NET Identity Provider. Just leaving it here for the next guy/gal :)

void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (this.User.Identity.IsAuthenticated)
        {
            WisewomanDBContext db = new WisewomanDBContext();

            // get user
            var userId = User.Identity.GetUserId();
            ApplicationUser user = db.Users.Find(userId);

            // has their password expired?
            if (user != null && user.PasswordExpires <= DateTime.Now.Date
                && !Request.Path.EndsWith("/Manage/ChangePassword"))
            {
                Response.Redirect("~/Manage/ChangePassword");
            }

            db.Dispose();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文