WindowsIdentity.GetCurrent()可以返回null吗?

14

ReSharper会提醒我在 NullReferenceException 的可能性。

WindowsIdentity windowsIdentity = new WindowsIdentity(WindowsIdentity.GetCurrent().Token);

我查阅了MSDN文档,但没有看到任何有关此事的提及。而且这也说不通,因为如果你运行一个可执行文件,你必须已经登录。

这只是ReSharper的搜索模式吗?

4个回答

21

使用ILSpy,您可以查看GetCurrentGetCurrentInternal的反编译版本,GetCurrent调用了后者。

GetCurrent:

public static WindowsIdentity GetCurrent()
{
    return WindowsIdentity.GetCurrentInternal(TokenAccessLevels.MaximumAllowed, false);
}

GetCurrentInternal:

internal static WindowsIdentity GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)
{
    int errorCode = 0;
    bool flag;
    SafeTokenHandle currentToken = WindowsIdentity.GetCurrentToken(desiredAccess, threadOnly, out flag, out errorCode);
    if (currentToken != null && !currentToken.IsInvalid)
    {
        WindowsIdentity windowsIdentity = new WindowsIdentity();
        windowsIdentity.m_safeTokenHandle.Dispose();
        windowsIdentity.m_safeTokenHandle = currentToken;
        return windowsIdentity;
    }
    if (threadOnly && !flag)
    {
        return null;
    }
    throw new SecurityException(Win32Native.GetMessage(errorCode));
}

由于在从GetCurrent调用时threadOnly始终为false,而且currentToken必须对其他返回语句有效,我不认为您会面临获取空的WindowsIdentity的风险。


6

ReSharper应该能够处理这个。

在目录<ReSharper安装目录>\v7.1\Bin\ExternalAnnotations\.NETFramework\mscorlib中,外部注释文件Nullness.Manual.xml定义了以下注释:

<!-- RSRP-328266 -->
<member name="M:System.Security.Principal.WindowsIdentity.GetCurrent">
  <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>
<member name="M:System.Security.Principal.WindowsIdentity.GetCurrent(System.Boolean)">
  <attribute ctor="M:JetBrains.Annotations.ContractAnnotationAttribute.#ctor(System.String)">
    <argument>false=&gt;notnull</argument>
  </attribute>
</member>
<member name="M:System.Security.Principal.WindowsIdentity.GetCurrent(System.Security.Principal.TokenAccessLevels)">
  <attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</member>

然而,我还收到了关于可能的 NullReferenceException 在 WindowsIdentity.GetCurrent() 上的警告。由于某种原因,ReSharper 没有识别其自己的外部注释属性。如果这是一个已知的错误,或者如果有解决此问题的方法,请回复。


2
好的,我已经帮您谷歌搜索了一下:) 这似乎是我们的小朋友: http://youtrack.jetbrains.com/issue/RSRP-328266 - Noich
没错。上述注释应该修复328266(因此在XML片段的第一行有注释),但出于某种原因,修复似乎没有起作用。如果这表明我的R#设置或配置存在问题,请详细说明。 - John Beyer
我真的不知道 :) 你需要跟他们的QA联系。 - Noich

2

这似乎是ReSharper的错误报告。

GetCurrent的MSDN页面没有提到在任何情况下返回null

正如你所指出的,必须有一个当前用户(某种类型的用户),因此如果您有权限,它应该始终返回有效对象。

它可能会引发SecurityException,但那是不同的错误,而且您的代码无论如何都会失败。如果这是一种可能性,那么您可能需要重新排列您的代码:

WindowsIdentity currentIdentity = null;
try
{
    currentIdentity = WindowsIdentity.GetCurrent();
    // Carry on if there's nothing you can do
    WindowsIdentity newIdentity = new WindowsIdentity(currentIdentity.Token);
}
catch (SecurityException ex)
{
    // Do something, logging, display error etc.
}

1
根据拆卸,可能会返回null
参见:GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly) 免责声明:我太懒了,不想分析具体条件 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接