2013-04-14 58 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

相關問題