0

我有一个asp.net应用程序,需要使用表单身份验证(Windows身份验证不是具有给定要求的选项)将用户登录到Active Directory中。检查Active Directory密码是否与cookie不同

我节省身份验证Cookie像这样:

if (Membership.ValidateUser(model.UserName, model.Password)) 
{ 
    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
} 

这个伟大的工程,但该Cookie认证,即使他们改变他们的Active Directory用户输入密码。

有没有办法判断用户的密码是否已更改?

我使用asp.net MVC3和.NET 4

我已经试过

如果觉得这样的代码应该工作,但是从来没有HttpWebResponse包含任何cookie。不太确定我做错了什么。

回答

2

您的代码应阅读

if (Membership.ValidateUser(model.UserName, model.Password)) 
{ 
    string userData = DateTime.Now.ToString(); 

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, 
    username, 
    DateTime.Now, 
    DateTime.Now.AddMinutes(30), 
    isPersistent, 
    userData, 
    FormsAuthentication.FormsCookiePath); 

    // Encrypt the ticket. 
    string encTicket = FormsAuthentication.Encrypt(ticket); 

    // Create the cookie. 
    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); 
} 

现在,认证用户

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.value); 
if (DateTime.Parse(ticket.UserData) > Membership.GetUser().LastPasswordChangedDate) 
{ 
    FormsAuthentication.SignOut(); 
    FormsAuthentication.RedirectToLoginPage(); 
} 
+0

感谢的时候,但是这仍然没有解决我的问题。我的问题不在于创建cookie,它创建得很好(通过Chrome的设置进行验证)。我只是无法检索它。此外,我假设您的请求对象是System.Web.Mvc中包含的HttpRequestBase对象,但只返回没有TimeStamp属性的HttpCookies。 –

+0

明白了..第二个代码段需要更新。给我一些时间。 – Ramesh

+0

@CavynVonDeylen更新。创建cookie时,我们将时间戳添加为用户数据。下一次请求到达您的应用程序时,cookie会出现在请求中,我们正在检索它并将日期时间存储在userdata中,并与lastpasswordchangedatetime – Ramesh

相关问题