2013-04-14 2 views
12

ReSharper에서 내가 MSDN의 문서에보고하지만,이에 대한 언급을 보지 못했다WindowsIdentity.GetCurrent()가 null을 반환 할 수 있습니까?

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

에 가능한 NullReferenceException에 대해 나에게 경고합니다. 또한 실행 파일을 실행하면 로그온해야하므로 이해가되지 않습니다.
이것은 단지 ReSharper 검색 패턴입니까?

답변

19

이 ILSpy 사용을 해부 너무 게으른 오전, 당신은 GetCurrentGetCurrentInternal의 디 컴파일 된 버전, GetCurrent 통화를 볼 수 있습니다 . 결과는 다음과 같습니다

에 getCurrent :

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

GetCurrentInternal : GetCurrent에서 전화 및 currentToken 다른 유효해야하는 경우 threadOnly 이후

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

은 항상 false입니다 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)

면책 조항 : 내가 특정 조건 :

5

ReSharper 이 처리해야합니다. 디렉토리> \ 7.1을 설치 디렉토리 < ReSharper에서에서

\ 빈 \ 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

관련 문제