简体   繁体   English

C#DirectoryEntry对象安全性属性引发空引用异常

[英]C# DirectoryEntry object security property throwing a null reference exception

I'm trying to trace an application error and I'm no whiz on the WinNT provider side. 我正在尝试跟踪应用程序错误,但在WinNT提供程序方面却丝毫不为过。 Here's the code I'm debugging: 这是我正在调试的代码:

 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();
        }
    }

When I debug this code and run "? New DirectoryEntry("WinNT://DOMAIN/User")" where DOMAIN and User are (supposedly) valid entries, the window returns the following for the ObjectSecurity line: 当我调试此代码并运行“?New DirectoryEntry(“ WinNT:// DOMAIN / User”)”(其中DOMAIN和User是(据说)有效条目)时,该窗口为ObjectSecurity行返回以下内容:

ObjectSecurity: 'new DirectoryEntry("WinNT://[DOMAIN]/[USER]").ObjectSecurity' threw an exception of type 'System.NullReferenceException' ObjectSecurity:'new DirectoryEntry(“ WinNT:// [DOMAIN] / [USER]”).ObjectSecurity'引发了类型为'System.NullReferenceException'的异常

I've seen the application throw an error of "The network path was not found." 我已经看到该应用程序抛出错误“找不到网络路径”。 I have only been able to replicate this error once and not again. 我只能一次又一次地复制此错误。 Unfortunately, it happens almost every time for 2 production users, but never happens in the test environment. 不幸的是,几乎每两个生产用户都会发生这种情况,但是在测试环境中却从未发生过。

Has anyone seen this situation before? 有人见过这种情况吗? Or know where to look to track it down? 还是知道在哪里寻找它?

Looks like the value for a[0] or a[1] is null. 看起来a [0]或a [1]的值为空。 Did you verify that those values are correct and that the Domain and the User exist ? 您是否验证了这些值正确并且域和用户存在? You should debug or log the values for a[0] or a[1] and try to find the error. 您应该调试或记录a [0]或a [1]的值,然后尝试查找错误。

This ended up getting fixed by removing the DirectoryEntry line and instead referencing just the HttpContext.Current.User.Identity.Name.Split('\\') for the username. 通过删除DirectoryEntry行并最终仅对用户名引用HttpContext.Current.User.Identity.Name.Split('\\')来解决此问题。 It doesn't matter that it only returns the username rather than the full name of the logged on user. 没关系,它只返回用户名,而不返回登录用户的全名。 Apparently it related to a permissions issue with AD that our system admins would rather not deal with, so removing the code ended up providing the simplest solution. 显然,这与我们系统管理员不愿处理的AD权限问题有关,因此删除代码最终提供了最简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM