Active Directory身份验证

时间:2011-08-26 07:07:54

标签: asp.net authentication active-directory

我在asp.net中创建了一个Web应用程序。在我的项目中,身份验证是通过匹配数据库中的用户名和密码完成的。但是现在客户端在Active Directory身份验证的帮助下要求我在应用程序中自动登录。客户要求建议我使用AD中用户的电子邮件ID进行身份验证。

我尝试在AD中获取记录,我可以获取用户的全名,但我无法获取电子邮件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但它生成错误Coulnot搜索客户端服务器中的记录..

2 个答案:

答案 0 :(得分: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>

希望这有帮助。

答案 1 :(得分:0)

为了阅读AD数据,我使用这个类。它是为我们的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;
}
相关问题