2012-06-27 38 views
0

在我的應用程序中,我正在做的事情,用戶可以從我的應用程序控制他/她的本地Windows用戶帳戶,即創建用戶,設置/刪除密碼,更改密碼也可以從我的應用程序調用密碼過期策略。現在,在這一點上,我需要弄清楚如果用戶想在下次登錄時更改密碼,那麼會發生什麼情況。由於許多論壇和博客說這個,我沒有相應的編碼:我想設置「密碼必須在下次登錄時更改」標誌

調用密碼過期在下次登錄時

public bool InvokePasswordExpiredPolicy() 
    { 
     try 
     { 
      string path = GetDirectoryPath(); 
      string attribute = "PasswordExpired"; 
      DirectoryEntry de = new DirectoryEntry(path); 
      de.RefreshCache(new string[] { attribute }); 
      if(de.Properties.Contains("PasswordExpired")) 
      de.Properties[attribute].Value = 1; 
      de.CommitChanges(); 
      return true; 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
    } 

招來密碼的有效期在下次登錄時。復位標誌

public bool ProvokePasswordExpiredPolicy() 
    { 
     try 
     { 
      string path = GetDirectoryPath(); 
      string attribute = "PasswordExpired"; 
      DirectoryEntry de = new DirectoryEntry(path); 
      de.RefreshCache(new string[] { attribute }); 
      de.Properties[attribute].Value = -1; 
      de.CommitChanges(); 
      return true; 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
    } 

檢查有關標誌是否被設置或不

public bool isPasswordPolicyInvoked() 
    { 
     try 
     { 
      string path = GetDirectoryPath(); 
      string attribute = "PasswordExpired"; 
      DirectoryEntry de = new DirectoryEntry(path); 
      de.RefreshCache(new string[] { attribute }); 
      int value = Convert.ToInt32(de.Properties[attribute].Value); 

      if (value == 1) 
       return true; 
      else 
       return false; 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
    } 

我使用WINNT得到的目錄路徑,而不是LDAP。我用下面的方法去得到目錄路徑。

private String GetDirectoryPath() 
    { 
     String uName = this.userName; 
     String mName = this.userMachine; 

     String directoryPath = "WinNT://" + mName + "/" + uName; 

     return directoryPath; 
    } 

有什麼我失蹤了嗎?在這裏幫助我。

注:首先,我用的pwdLastSet屬性設置爲0(上)和-1(對於關閉)拋出異常「目錄屬性不在屬性緩存中找到了」,後來我發現, WinNT不支持這個屬性,而是支持PasswordExpired需要設置1的標誌。這就是我所做的。

回答

3

如何使用System.DirectoryServices.AccountManagement代替,在這種情況下,您可以撥打下面的代碼:

UserPrincipal.Current.ExpirePasswordNow(); 
-3
de.Properties["passwordExpired"][0] = 1; <br> 
de.CommitChanges(); 
+0

這是不正確的答案,-1。 –

+0

定義此 private const Int32 PASSWORD_EXPIRED = 0x800000; 或將其存入您的用戶帳戶控制中。對不起沒有代碼,我的方法是高度抽象的圖書館的一部分,並沒有多大意義。 –

相關問題