2011-09-07 79 views
0

我有一個關於Active Directory的問題。我的項目已被託管在一臺服務器上。活動目錄已在另一臺服務器上維護。現在,我需要在員工登錄時在我的應用程序中使用AD身份驗證。由於我無法獲取ACtive Directory的記錄,所以在使用這兩種不同的服務時我完全困惑;我已經使用的代碼是:Asp.net中的Active Directory C#

string principal = this.Context.User.Identity.Name; 
string filter = string.Format("(&(ObjectClass=dev)(sAMAccountName={1}))", "dev", principal); 
string domain = "SOFTWARESERVER"; 
string[] properties = new string[] { "fullname","mail","sn" }; 
System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
string[] a = Context.User.Identity.Name.Split('\\'); 

DirectoryEntry ADEntry = new DirectoryEntry("LDAP://SOFTWARESERVER/DC=softageenapl,DC=com,DC=np"); 
DirectorySearcher searcher = new DirectorySearcher(ADEntry); 

searcher.SearchScope = SearchScope.Subtree; 
searcher.ReferralChasing = ReferralChasingOption.All; 
searcher.PropertiesToLoad.AddRange(properties); 
searcher.Filter = filter; 
SearchResult result = searcher.FindOne(); 
DirectoryEntry directoryEntry = result.GetDirectoryEntry(); 

string Name = ADEntry.Properties["Fullname"].Value.ToString(); 
string displayName = directoryEntry.Properties["displayName"][0].ToString(); 
string firstName = directoryEntry.Properties["givenName"][0].ToString(); 
string lastName = directoryEntry.Properties["sn"][0].ToString(); 
string email = directoryEntry.Properties["mail"][0].ToString(); 
+0

你可以編輯你的問題......它不是明確你真正想做什麼,你現在有2個服務器,你需要進行身份驗證? – balexandre

+0

其實這是我的客戶需求讓我的頭。我在一臺名爲鏈接服務器的服務器上託管了項目,項目在數據庫中比較用戶名和密碼的表單身份驗證模式下正常工作。但客戶端請求使用Active Directory身份驗證,並且此AD已在另一臺名爲Fileserver的服務器中維護。現在,如何在使用AD身份驗證幫助登錄系統時驗證用戶身份..... – Nhuren

+0

如果我可以訪問AD文件服務器中用戶的郵件地址,以便在計劃託管的鏈接服務器上進行身份驗證(另一臺服務器),它會爲我解決...所以任何解決方案 – Nhuren

回答

0

我知道這個問題是從2011年目前,我希望這段代碼可以幫助別人

(C#)添加這些引用:

using System.DirectoryServices; 
using System.DirectoryServices.AccountManagement; 

之後,你可以在你的應用程序中使用此代碼:

PrincipalContext p = new PrincipalContext(ContextType.Domain, "IP of the server"); 
bool Valid = p.ValidateCredentials("User", "password"); 

稱爲變量:瓦爾ID,會告訴你一個值如果logIn 是好的

看一看這個問題:是從here, StackOverflow,人們已經更詳細地解釋了這個話題(帶有Microsoft Active Directory的「logIn」)。

相關問題