2013-04-14 60 views
12

ReSharper的警告我有关可能NullReferenceException可以WindowsIdentity.GetCurrent()返回null?

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

我看了MSDN文档,但没有看到这个任何提及。此外,它是没有意义的,因为如果你运行一个可执行文件,你必须登录。
这只是一个ReSharper搜索模式?

回答

19

使用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)); 
} 

由于threadOnlyGetCurrent打电话时,和currentToken必须为其它有效始终是假的返回声明,我不认为你有风险收到null WindowsIdentity

2

这听起来像来自ReSharper的虚假报告。

MSDN page for GetCurrent在任何情况下都没有提及返回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)

免责声明:我懒得去解剖特定条件:)

5

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> 

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

+2

好吧,我为你搜索它:) 这似乎是我们的小朋友: http://youtrack.jetbrains.com/issue/RSRP-328266 – Noich

+0

没错。上面的注释应该修复328266(因此对XML片段的第一行进行注释),但无论出于何种原因,修复似乎都不起作用。 如果这表明我的R#设置或配置有问题,请详细说明。 –

+0

我真的不知道:)你将不得不把它与他们的质量保证。 – Noich

相关问题