[英]C# DirectoryEntry object security property throwing a null reference exception
我正在嘗試跟蹤應用程序錯誤,但在WinNT提供程序方面卻絲毫不為過。 這是我正在調試的代碼:
public string GetCurrentRequesterIdentity()
{
var a = HttpContext.Current.User.Identity.Name.Split('\\');
using (var adEntry = new DirectoryEntry("WinNT://" + a[0] + "/" + a[1]))
{
return adEntry.Properties["FullName"].Value.ToString();
}
}
當我調試此代碼並運行“?New DirectoryEntry(“ WinNT:// DOMAIN / User”)”(其中DOMAIN和User是(據說)有效條目)時,該窗口為ObjectSecurity行返回以下內容:
ObjectSecurity:'new DirectoryEntry(“ WinNT:// [DOMAIN] / [USER]”).ObjectSecurity'引發了類型為'System.NullReferenceException'的異常
我已經看到該應用程序拋出錯誤“找不到網絡路徑”。 我只能一次又一次地復制此錯誤。 不幸的是,幾乎每兩個生產用戶都會發生這種情況,但是在測試環境中卻從未發生過。
有人見過這種情況嗎? 還是知道在哪里尋找它?
看起來a [0]或a [1]的值為空。 您是否驗證了這些值正確並且域和用戶存在? 您應該調試或記錄a [0]或a [1]的值,然后嘗試查找錯誤。
通過刪除DirectoryEntry行並最終僅對用戶名引用HttpContext.Current.User.Identity.Name.Split('\\')來解決此問題。 沒關系,它只返回用戶名,而不返回登錄用戶的全名。 顯然,這與我們系統管理員不願處理的AD權限問題有關,因此刪除代碼最終提供了最簡單的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.