2017-04-06 28 views
0

LDAPを使用してASP.NET MVC 5で実装されたActive Directory認証を実装しました。私は、ユーザーのActive Directoryからユーザーのパスワードの有効期限を取得する方法は?

  1. アカウントロック(boolean)を取得する方法を知りたい
  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

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

最も簡単な方法は、のPInvokeのLogonUser Win32のAPI.eg にあるhttp://www.pinvoke.net/default.aspx/advapi32/LogonUser.html

も読むMSDNの参照:ご質問のためのhttps://msdn.microsoft.com/en-us/library/aa378184.aspx

例:私はSystem.Web.SecurityとSystem.DirectoryServicesの組み合わせを使用してそれを行うために管理https://www.codeproject.com/articles/18102/howto-almost-everything-in-active-directory-via-c

+0

私はSystem.Web.Securityを可能な限り使用したいと考えていました。私はAccountLockedを取得することができました。今私はオブジェクトのプロパティを取得できるようにオブジェクトとしてLDAP Activedirectoryをインスタンス化する方法を知る必要があります。どのように達成できるか誰にでも分かりますか? – user1166085

+0

私は自分の答えを変更し、例えばリンクを追加しました。 – Murad

+0

よろしくお願いします。私は別の方法でそれを解決することができました。私は答えをアップロードしました。あなたは一見することができます。 – user1166085

関連する問題