2013-04-14 11 views
12

ReSharperのは、私は、MSDNのドキュメントに見えたが、これの一切の言及を見ていないWindowsIdentity.GetCurrent()はnullを返しますか?

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

に可能NullReferenceExceptionについて私に警告します。また、実行可能ファイルを実行するとログオンする必要があるため、意味がありません。
これはちょうどReSharperの検索パターンですか?

答えて

19

を分析するのが面倒だ、あなたは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)); 
} 

threadOnlyのでGetCurrentから呼び出すときは常に偽である、とcurrentTokenは他のために有効である必要がありますreturn文、私はあなたが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)

免責事項:私は特定の条件:) ILSpyを使用して

5

ReSharper shouldこれを処理します。

< ReSharperのはディレクトリ> \ V7.1 \ビン\ 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

よく、私はあなたのためにそれをgoogled: これは私たちの小さな友人のようです: http://youtrack.jetbrains.com/issue/RSRP-328266 – Noich

+0

まさに。上記のアノテーションは328266(つまり、XMLスニペットの最初の行のコメント)を修正することになっていますが、何らかの理由で修正が機能していないようです。 R#の設定や構成に問題がある場合は、詳しく説明してください。 –

+0

私は本当に考えていない:)あなたはそのQAでそれを取る必要があります。 – Noich

関連する問題