2017-04-06 101 views
0

我已經使用LDAP在ASP.NET MVC 5中實現了Active Directory身份驗證。我想知道如何獲得用戶的如何從Active Directory獲取用戶的密碼到期日期?

  1. 帳戶鎖定(布爾)
  2. 密碼過期(布爾)
  3. 密碼到期日期(日期時間)

這是我當前的代碼:

using System.Web.Mvc; 
using System.Web.Security; 
using MvcApplication.Models; 

[HttpPost] 
public ActionResult Login(LoginModel model, string returnUrl) 
{ 
    if (!this.ModelState.IsValid) 
    { 
     return this.View(model); 
    } 

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

     if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
      && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
     { 
      return this.Redirect(returnUrl); 
     } 

     return this.RedirectToAction("Index", "Home"); 
    } 

    this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect."); 

    return this.View(model); 
} 

public ActionResult LogOff() 
{ 
    FormsAuthentication.SignOut(); 

    return this.RedirectToAction("Index", "Home"); 
} 
+0

我希望儘可能地使用System.Web.Security。通過我可以檢索AccountLocked。現在我需要知道如何將我的LDAP Activedirectory實例化爲對象,以便我可以獲取其對象屬性。任何人都知道如何實現? – user1166085

回答

1

我管理使用System.Web.Security和的System.DirectoryServices的組合來做到這一點。

public bool IsExpired(MembershipUser user, LoginModel model) 
{ 
    bool result = false; 

    string ldap = ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString; 

    DirectoryEntry rootEntry = new DirectoryEntry(ldap, model.UserName, model.Password, AuthenticationTypes.Secure); 

    DirectorySearcher mySearcher = new DirectorySearcher(rootEntry); 

    SearchResultCollection results; 
    string filter = "maxPwdAge=*"; 
    mySearcher.Filter = filter; 

    results = mySearcher.FindAll(); 
    long maxDays = 0; 
    if (results.Count >= 1) 
    { 
     Int64 maxPwdAge = (Int64)results[0].Properties["maxPwdAge"][0]; 
     maxDays = maxPwdAge/-864000000000; 
    } 

    long daysLeft = 0; 

    daysLeft = maxDays - DateTime.Today.Subtract(user.LastPasswordChangedDate).Days; 

    if (daysLeft <0) 
    { 
     result = true; 
    } else 
    { 
     if (daysLeft<=14) 
     { 
      this.Expiring = true; 
      this.ExpiringString = String.Format("You must change your password within" + " {0} days", daysLeft); 
     }  
     else 
     { 
      this.Expiring = false; 
     }  
    } 

    return result; 
} 
2
+0

我希望儘可能使用System.Web.Security。通過我可以檢索AccountLocked。現在我需要知道如何將我的LDAP Activedirectory實例化爲對象,以便我可以獲取其對象屬性。任何人都知道如何實現? – user1166085

+0

我改變了我的答案,並添加了鏈接例如。 – Murad

+0

嘿謝謝你的幫助。我設法解決它使用另一種方法。我已經上傳了答案。你可以看看。 – user1166085

相關問題