2013-08-26 90 views
-1

我有一個使用Windows身份驗證的.NET MVC 4應用程序。某些用戶是管理員,需要能夠代表其他用戶輸入數據。使用Windows身份驗證時驗證另一個用戶

我有一個文本框,管理員輸入另一個用戶的名字。如何檢查以確認輸入的文本是現有的Windows用戶名?

回答

0

你可以使用FindByIdentity方法:

string username = "Some username you retrieved from the TextBox"; 

using (var ctx = new PrincipalContext(ContextType.Domain, "YOUR_DOMAIN")) 
using (var user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, username)) 
{ 
    bool userExists = user != null; 
    // here you know whether the user exists or not 
} 
+3

任何理由downvote?請在回答問題時留下評論,說明您爲什麼認爲此答案錯誤。 –

+0

不知道這個問題和這些答案的所有仇恨背後是什麼,但這對我有效。謝謝。 –

-1

您可以查詢您的組織的活動目錄這一點。

DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName"); 
DirectorySearcher Dsearch = new DirectorySearcher(entry); 
String Name="Richmond"; 
dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))"; 

看到這篇文章: http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory

+1

其實有一個比DirectoryEntry更新的API:http://msdn.microsoft.com/en-us/library/bb344891.aspx –

相關問題