2013-04-14 9 views
12

ReSharperWindowsIdentity.GetCurrent() वापस शून्य कर सकते हैं?

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

में संभावित NullReferenceException के बारे में मुझे चेतावनी दी है मैं MSDN दस्तावेज़ में देखा है, लेकिन इस का कोई उल्लेख नहीं देखा। साथ ही, यह समझ में नहीं आता है कि यदि आप निष्पादन योग्य चलाते हैं, तो आपको लॉग ऑन करना होगा।
क्या यह सिर्फ एक रेसपर खोज पैटर्न है?

उत्तर

19

ILSpy का उपयोग करते हुए टुकड़े करना आलसी हूँ, आप GetCurrent और GetCurrentInternal की एक de-संकलित संस्करण है, जो 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 अन्य के लिए वैध होना चाहिए है वापसी विवरण, मुझे नहीं लगता कि आपको शून्यप्राप्त करने का खतरा है।

2

यह रीशेपर से झूठी रिपोर्ट की तरह लगता है।

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

disassembly के अनुसार, null लौटे जा सकता है।

देखें: GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)

अस्वीकरण: मैं भी विशिष्ट हालत :)

5

रीशेपर इसे संभालना चाहिए।

dir> \ v7.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 के बारे में चेतावनी भी प्राप्त कर रहा हूं। किसी कारण से, रीशेपर अपने बाहरी एनोटेशन विशेषताओं को पहचान नहीं रहा है। यदि यह ज्ञात बग है, या यदि इस समस्या के लिए कोई फिक्स है, तो कृपया उत्तर दें।

+2

अच्छी तरह से, मैंने इसे आपके लिए googled :) यह हमारा छोटा मित्र लगता है: http://youtrack.jetbrains.com/issue/RSRP-328266 – Noich

+0

बिल्कुल।उपर्युक्त एनोटेशन 328266 को ठीक करना है (इसलिए एक्सएमएल स्निपेट की पहली पंक्ति पर टिप्पणी), लेकिन किसी भी कारण से, फिक्स काम नहीं कर रहा है। यदि यह मेरी आर # सेटिंग्स या कॉन्फ़िगरेशन में कोई समस्या इंगित करता है, तो कृपया विस्तृत करें। –

+0

मुझे वास्तव में कोई जानकारी नहीं है :) आपको इसे अपने क्यूए के साथ ले जाना होगा। – Noich

संबंधित मुद्दे