检查 Active Directory 密码是否与 cookie 不同

Check if Active Directory password is different from cookie(检查 Active Directory 密码是否与 cookie 不同)
本文介绍了检查 Active Directory 密码是否与 cookie 不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 asp.net 应用程序,它需要使用表单身份验证将用户登录到 Active Directory(Windows 身份验证不是具有给定要求的选项).

I have an asp.net app which needs to log users into Active Directory using forms authentication (windows authentication isn't an option with the given requirements).

我像这样保存身份验证 cookie:

I'm saving authentication cookies like so:

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

这很有效,除了即使用户更改了 Active Directory 密码后,cookie 也会对用户进行身份验证.

This works great, except that the cookie authenticates the user even after they change their Active Directory password.

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

Is there a way to tell if the user's password has changed?

我在 .NET 4 中使用 asp.net MVC3

I'm using asp.net MVC3 with .NET 4

我的尝试

如果觉得这段代码应该可以工作,但是 HttpWebResponse 永远不会包含任何 cookie.不太确定我做错了什么.

If feel like this code should work, however the HttpWebResponse never contains any cookies. Not quite sure what I'm doing wrong.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Request.Url);
request.CookieContainer = new CookieContainer();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Cookie authCookie = response.Cookies["AuthCookie"];
if (authCookie.TimeStamp.CompareTo(Membership.GetUser().LastPasswordChangedDate) < 0)
{
    authCookie.Expired = true;
}

推荐答案

你的代码应该阅读

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();
}

这篇关于检查 Active Directory 密码是否与 cookie 不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Connection string to connect to Active Directory using LDAP(使用 LDAP 连接到 Active Directory 的连接字符串)
Redirect user to custom login page when using Azure AD(使用 Azure AD 时将用户重定向到自定义登录页面)
Poor Performance with WindowsTokenRoleProvider(WindowsTokenRoleProvider 的性能不佳)
How do you impersonate an Active Directory user in Powershell?(如何在 Powershell 中模拟 Active Directory 用户?)
What Active Directory field do I use to uniquely identify a user?(我使用哪个 Active Directory 字段来唯一标识用户?)
LDAP - Retrieve a list of all attributes/values?(LDAP - 检索所有属性/值的列表?)