2010-11-02 53 views
0

我有一個內部網,通過活動目錄獲取當前登錄用戶。當用戶被鎖定時,他們會得到一個Windows提示輸入他們的用戶名和密碼。有沒有辦法讓我知道這一點,並將它們重定向到要求他們再次輸入憑據的頁面,或者告訴他們他們的帳戶可能被鎖定並聯系幫助臺?asp.net活動目錄內部網

回答

0

在您的應用程序,一旦你抓住是登錄用戶在下面做

public bool IsAccountLocked(string sUserName) 
{ 
    UserPrincipal oUserPrincipal = GetUser(sUserName); 
    return oUserPrincipal.IsAccountLockedOut(); 
} 

public UserPrincipal GetUser(string sUserName) 
{ 
    PrincipalContext oPrincipalContext = GetPrincipalContext(); 

    UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName); 
    return oUserPrincipal; 
} 

public PrincipalContext GetPrincipalContext() 
{ 
    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword); 
    return oPrincipalContext; 
} 

這是使用System.DirectoryServices.AccountManagement的IsAccountLocked方法僅使用的System.DirectoryServices你能做到這一點

public bool IsAccountLocked(DirectoryEntry oDE) 
{ 
    return Convert.ToBoolean(oDE.InvokeGet("IsAccountLocked")); 
} 
public DirectoryEntry GetUser(string sUserName) 
{ 
    //Create an Instance of the DirectoryEntry 
    oDE = GetDirectoryObject(); 

    //Create Instance fo the Direcory Searcher 
    oDS = new DirectorySearcher(); 

    oDS.SearchRoot = oDE; 
    //Set the Search Filter 
    oDS.Filter = "(&(objectClass=user)(sAMAccountName=" + sUserName + "))"; 
    oDS.SearchScope = SearchScope.Subtree; 
    oDS.PageSize = 10000; 

    //Find the First Instance 
    SearchResult oResults = oDS.FindOne(); 

    //If found then Return Directory Object, otherwise return Null 
    if (oResults != null) 
    { 
     oDE = new DirectoryEntry(oResults.Path, sADUser, sADPassword, AuthenticationTypes.Secure); 
     return oDE; 
    } 
    else 
    { 
     return null; 
    } 
} 
private DirectoryEntry GetDirectoryObject() 
    { 
     oDE = new DirectoryEntry(sADPath, sADUser, sADPassword, AuthenticationTypes.Secure); 
     return oDE; 
    } 

的全面實施,你可以去 http://anyrest.wordpress.com/2010/06/28/active-directory-c/http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

+0

謝謝,我會試試這個 – Migs 2010-11-02 22:20:38