我正在使用 ASP.NET 成员身份验证我的 Web 应用程序。这对我很有用。我现在必须实现密码过期。
如果密码已过期,则应将用户重定向到ChangePassword屏幕,并且不应允许在不更改密码的情况下访问应用程序的任何其他部分。
有很多aspx页面。如果密码已过期,一种解决方案可能是重定向到每个 aspx 的ChangePassword屏幕OnInit。有没有其他解决方案或建议。
谢谢,杰
我正在使用 ASP.NET 成员身份验证我的 Web 应用程序。这对我很有用。我现在必须实现密码过期。
如果密码已过期,则应将用户重定向到ChangePassword屏幕,并且不应允许在不更改密码的情况下访问应用程序的任何其他部分。
有很多aspx页面。如果密码已过期,一种解决方案可能是重定向到每个 aspx 的ChangePassword屏幕OnInit。有没有其他解决方案或建议。
谢谢,杰
除了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");
}
}
}
您可以在 global.asax 中为 HttpApplication.PostAuthenticateRequest 事件添加事件处理程序并在那里处理重定向。
在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");
}
}
}
大约一个小时就实现了这个,不需要修改你的基本页面。这是你必须做的:
响应LoggingIn成员控制事件
在会员数据库中找到用户并获取LastPasswordChangedDate
使用 TimeSpan,将其与当前日期进行比较,并确定上次更改密码的时间是否超过所需天数。我从 web.config 得到这个值
如果过期,重定向到ChangePassword屏幕
我来这里是为了寻找解决方案,但我目前的技术是 ASP.NET MVC。所以为了帮助别人:你可以扩展AuthorizeAttribute, 和 overrideOnAuthorization方法,像这样:
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来检索上面代码中的 Controller 和 Action 名称。
用这个属性标记除“ AccountController”之外的所有控制器。这样做,密码过期的用户将无法浏览该网站。
这是我在该主题上发表的一篇文章,其中有一些加分:
我使用了上面的代码,只对其稍作修改以使用 .NET 身份提供程序在 Asp.NET (4.5) MVC5 中实现。把它留在这里给下一个家伙/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();
}
}