從 asp.net Web 應用程序的活動目錄中獲取用戶的全名 (Get user's full name from active directory in asp.net web application)


問題描述

從 asp.net Web 應用程序的活動目錄中獲取用戶的全名 (Get user's full name from active directory in asp.net web application)

我正在嘗試從活動目錄中獲取給定用戶的全名。這段代碼可以在我的電腦上運行,但是當我把它放在服務器上時它會拋出異常:

找不到網絡路徑。

代碼是:

DirectoryEntry obDirEntry = null;
        try
        {
            obDirEntry = new DirectoryEntry("WinNT://" + "domain" + "/" + Environment.UserName);
            System.DirectoryServices.PropertyCollection coll = obDirEntry.Properties;
            object obVal = coll["FullName"].Value;
            Response.Write(obVal);
        }
        catch (Exception ex)

        {
           Response.Write(ex.Message);
        }

知道如何修復它以便它也可以在服務器上運行嗎?或者也許我可以通過其他方式獲取給定用戶名的全名?我是否需要以某種方式使用 LDAP?


參考解法

方法 1:

There can be many issues here is what I found out

  1. Your new Directory object is pointing to WINNT I guess you need to use LDAP address for this one i.e.

    new DirectoryEntry("LDAP://" + sADServer + "/" + sNewOUPath, sADUser, sADPassword, AuthenticationTypes.Secure);

  2. In your webserver you need to change the user in your App Pool, and that user should have the proper rights to AD

  3. You can also edit your web.config to do <identity impersonate="true" /> and mage sure that the one that runs the page on the web server has the proper permission to AD

for a full implementation referenc of AD refer to this one http://anyrest.wordpress.com/2010/02/01/active‑directory‑objects‑and‑c/

方法 2:

I just faced with this problem too, and I found another solution. In my case I just added ".local" postfix after domain name. I.e. I've done something like this:

DirEntry = new DirectoryEntry("WinNT://" + "domain.local" + "/" + Environment.UserName);

BR, Vladimir

(by Greg OksRaymunduser2173535)

參考文件

  1. Get user's full name from active directory in asp.net web application (CC BY‑SA 3.0/4.0)

#ASP.NET #ldap #active-directory #DNS






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論