2011-08-26 64 views
1

我已经在asp.net中制作了一个web应用程序。在我的项目中,通过在数据库中匹配用户名和密码来完成认证。但现在客户端要求我在应用程序中使用Active Directory认证。客户请求建议我使用AD中用户的电子邮件ID进行身份验证。活动目录认证

我想在广告中获取的记录,我可以获取用户的全名,但我无法得到的电子邮件ID,

我试过代码:

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
    string[] a = Context.User.Identity.Name.Split('\\'); 

    System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]); 
    string Name = ADEntry.Properties["FullName"].Value.ToString(); 

进一步我使用DirectorySearcher,但它genterates错误,Coulnot搜索客户端服务器中的记录..

回答

0

对于读取AD数据,我使用这个类。它是为我们的广告设置的,但基本上,你可以通过所有你想查找的“字段”,在params中。 但是你需要知道什么字段包含电子邮件地址。 Sysinternals为浏览AD提供了一个非常好的工具,可以找出您要查找的内容,称为ADExplorer。

但我不明白你为什么需要在AD看?您是否可以不假设用户已经通过身份验证,是否在网络上,然后依赖Windows身份?

public static Hashtable GetAttributes(string initials, params string[] Attribute) 
{ 
    DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://ADNAME"); 
    DirectorySearcher ADSearcher = new DirectorySearcher(directoryEntry); 
    ADSearcher.Filter = "(sAMAccountName=" + initials + ")"; 
    foreach (string para in Attribute) 
    { 
     ADSearcher.PropertiesToLoad.Add(para); 
    } 
    SearchResult adSearchResult = ADSearcher.FindOne(); 

    Hashtable hshReturns = new Hashtable(); 
    foreach (string para in Attribute) 
    { 
     string strReturn = ""; 
     if (adSearchResult.Properties[para].Count == 0) 
      strReturn = ""; 
     else 
      strReturn = ((ResultPropertyValueCollection)adSearchResult.Properties[para])[0].ToString(); 
     hshReturns.Add(para, strReturn); 
    } 
    return hshReturns; 
} 
1

在制作公司门户时,我的确有相同的情况。 如果他们不想让你进入他们的AD,那么你可以做的是要求获得访问门户的人的NTLogins。制作一个简单的表格,其中包含NTLogin,并使用从中访问门户的系统进行身份验证。 查看我使用的示例代码。

// Checking if the user opening this page is listed in the allowed user list against their NT login. 
     String sUser = Request.ServerVariables["LOGON_USER"].ToLower(); 
     sUser = sUser.Replace("wt\\", ""); 

     //Authentication using a custom auth method. 
     DatabaseOperations authenticateUser = new DatabaseOperations(); 
     if (!authenticateUser.authenticate(sUser)) 
     { 
      //unauthorized users will be redirected to access denied page. 
      Server.Transfer("AccessDenied.aspx", true); 
     } 

,并确保您有认证方式为窗口在你的web.config文件

<authentication mode="Windows"></authentication> 

希望这有助于。