2013-04-14 14 views
12

ReSharper cảnh báo tôi về một thể NullReferenceException trongcó thể WindowsIdentity.GetCurrent() trả về null không?

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

tôi nhìn vào MSDN doc nhưng không thấy bất kỳ đề cập đến điều này. Ngoài ra, nó không có ý nghĩa vì nếu bạn chạy một tệp thực thi, bạn phải đăng nhập.
Đây có phải chỉ là mẫu tìm kiếm ReSharper không?

Trả lời

19

Sử dụng ILSpy, bạn có thể nhìn vào một phiên bản de-biên dịch của GetCurrentGetCurrentInternal, mà GetCurrent cuộc gọi . Kết quả là:

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

Kể từ threadOnly phải lúc nào cũng sai khi gọi từ GetCurrent, và currentToken phải có giá trị cho người khác return statement, tôi không nghĩ rằng bạn có nguy cơ bị null WindowsIdentity.

2

Có vẻ như báo cáo sai từ ReSharper.

MSDN page for GetCurrent không đề cập đến việc trả lại null trong mọi trường hợp.

Khi bạn chỉ ra, phải có người dùng hiện tại (loại này hay loại khác), vì vậy, điều này luôn phải trả lại đối tượng hợp lệ - nếu bạn có quyền.

Nó có thể tăng SecurityException, nhưng đó là một lỗi khác và mã của bạn sẽ không thành công. Nếu đây là một khả năng thì bạn có thể muốn sắp xếp lại mã của bạn:

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

Theo tháo gỡ, null có thể được trả lại.

Xem: GetCurrentInternal(TokenAccessLevels desiredAccess, bool threadOnly)

Disclaimer: tôi quá lười biếng để phân tích các điều kiện cụ thể :)

5

ReSharper nên xử lý việc này.

Trong thư mục < ReSharper cài đặt dir> \ v7.1 \ Bin \ ExternalAnnotations \ .NETFramework \ mscorlib, các chú thích bên ngoài nộp Nullness.Manual.xml định nghĩa các chú thích như sau:

<!-- 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> 

Tuy nhiên, tôi tôi cũng nhận được cảnh báo về NullReferenceException có thể có trên WindowsIdentity.GetCurrent(). Vì một số lý do, ReSharper không nhận ra các thuộc tính chú thích bên ngoài của nó. Nếu đây là lỗi đã biết hoặc nếu có sự cố xảy ra với sự cố này, vui lòng trả lời.

+2

tốt, tôi googled nó cho bạn :) Điều này có vẻ là người bạn nhỏ của chúng tôi: http://youtrack.jetbrains.com/issue/RSRP-328266 – Noich

+0

Chính xác.Các chú thích ở trên được cho là sửa chữa 328266 (do đó nhận xét trên dòng đầu tiên của đoạn mã XML), nhưng vì bất kỳ lý do nào, sửa chữa dường như không hoạt động. Nếu điều này cho thấy sự cố trong cài đặt hoặc cấu hình R # của tôi, vui lòng giải thích. –

+0

Tôi thực sự không có ý tưởng :) Bạn sẽ phải mang nó với QA của họ. – Noich