簡體   English   中英

如何配置ASP.Net網站以訪問Active Directory

[英]How to configure ASP.Net website to have access to Active Directory

我正在嘗試創建一個Intranet網站,該網站可以根據用戶的Active Directory用戶名查找用戶的電子郵件地址。

我的web.config中包含以下內容:

<authentication mode="Windows"/>
<identity impersonate="true"/>

我可以通過以下方式獲得用戶UserName:

Environment.UserName

在本地主機上運行,​​以下代碼使我可以查詢廣告並獲取電子郵件:

public string GetADUser(string userName)
{            
    DirectoryEntry entry = new DirectoryEntry();

    // get a DirectorySearcher object
    DirectorySearcher search = new DirectorySearcher(entry);

    // specify the search filter
    search.Filter = "(&(objectClass=user)(anr=" + userName + "))";

    // specify which property values to return in the search  
    search.PropertiesToLoad.Add("mail");    // smtp mail address

    // perform the search
    SearchResult result = search.FindOne();

    string email = string.Empty;

    if (result != null)
    {
        if (result.Properties["mail"].Count == 1)
        {
            email = result.Properties["mail"][0].ToString();
        }
        else
        {
            email = "no email";
        }
    }
    else
    {
        email = "not found";
    }

    return email;
}

很好,此代碼默認情況下使用我的憑據進行身份驗證,並允許我傳遞用戶名並查找用戶的電子郵件地址。

但是,當我將此測試代碼上傳到服務器時,如果我從localhost以外的任何地方瀏覽到該站點,該代碼將停止工作。

[COMException (0x80072020): An operations error occurred.]

谷歌搜索顯示我有權限問題。

為了解決這個問題,我嘗試將應用程序池標識設置為我的憑據,但這仍然不允許代碼搜索AD。

網站身份驗證在IIS中的配置如下(啟用項標記為<<):

Anonymous Authentication:Disabled
ASP.NET Impersonation:Enabled  <<
Basic Authentication:Disabled
Digest Authentication:Disabled
Forms Authentication:Disabled
Windows Authentication:Enabled  <<

甚至有可能做我想做的事情? 我想念什么?

好的,我發現了問題。

在這種情況下,在IIS中ASP.NET Impersonation:Enabled和我的Web.Config與我配置的應用程序池標識沖突。 (我認為)。

一旦我將應用程序池標識設置為使用經過身份驗證的適當帳戶來運行以查詢AD,禁用了模擬,並退出了Windows Authentication:Enabled ,就能夠使網站查詢AD,而無需在代碼中傳遞任何憑據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM