2013-06-20 26 views
0

我需要檢查我的應用程序中的用戶是否是活動目錄中的活動用戶。 我需要在用戶別名失效時發送通知。檢查用戶列表是否對C#中的adfs有效

在大多數示例中,我看到每次只使用LDAP來驗證一次用戶,而使用LDAP將需要很長時間的大量用戶。

有沒有什麼辦法可以通過發送用戶列表和驗證來驗證,以便它更快?

謝謝。

+0

你是什麼意思,「活躍」什麼是無效?他們不是在一個特定的組? –

回答

0

開箱即用的ADFS,沒有。

這聽起來像你應該從你的應用程序調用。使用AD C#API。

參照Howto: (Almost) Everything In Active Directory via C#

或(在某些情況下)Everything in Active Directory via C#.NET 3.5 (Using System.DirectoryServices.AccountManagement)

+0

這已經過時了。有一個更簡單的方法。 –

+0

哪是?你的意思是AccountManagement?這只是一個子集。所以不,在某些情況下,它不是過時的。 – nzpcmad

+0

是的,AccountManagement。我認爲這個子集足夠了,編碼起來會容易得多。 –

0

與.net 3.5開始有System.DirectoryServices.AccountManagement

我編寫類似

public List<string> InvalidUsernames (List<string> usernames) 
{ 
    var result = new List<string>(); 
    var domainName = "OkieDokie"; 
    var ldapContext = new PrincipalContext(ContextType.Domain, domainName); 
    foreach (var username in usernames) 
    { 
     var user = UserPrincipal.FindByIdentity(ldapContext, username); 
     if (user == null) //null means it couldn't be found 
     { 
      result.Add(username); 
     } 
    } 
    return result; 
} 

但是這一切都取決於什麼你考慮主動/無效。在如果您可以檢查user.AccountExpirationDate(?date)或user.Enabled(?bool)。

或者如果你有所有這些共同的組,你可以取代以前的foreach和使用:

var usersGroup = UsernamesInGroup("theONEgroup"); 
foreach (var username in usernames) 
{ 
    var user = UserPrincipal.FindByIdentity(ldapContext, username); 
    if (user == null) //null means it couldn't be found 
    { 
     result.Add(username); 
    } 
} 

public List<string> UsernamesInGroup(string groupName) 
{ 
    GroupPrincipal grupo = GroupPrincipal.FindByIdentity(MainOU, groupName); 
    return UsernamesInGroup(group); 
} 

public List<string> UsernamesInGroup(GroupPrincipal gp) 
{ 
    List<string> userNames = new List<string>(); 
    var principalsInGroup = gp.GetMembers(true); 
    foreach (Principal principal in principalsInGroup) 
    { 
     if (principal.StructuralObjectClass == "user") 
     { 
      userNames.Add(principal.SamAccountName); 
     } 
    } 
    return userNames; 
}