resharper – 可以WindowsIdentity.GetCurrent()返回null吗?

前端之家收集整理的这篇文章主要介绍了resharper – 可以WindowsIdentity.GetCurrent()返回null吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
ReSharper警告我可能的NullReferenceException
  1. WindowsIdentity windowsIdentity = new WindowsIdentity(WindowsIdentity.GetCurrent().Token);

我查看了MSDN文档,但没有看到任何提及.此外,它没有意义,因为如果您运行可执行文件,则必须先登录.
这只是一个ReSharper搜索模式吗?

使用ILSpy,您可以查看GetCurrent和GetCurrentInternal的解编译版本,GetCurrent调用GetCurrentInternal.
结果是:

GetCurrent:

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

GetCurrentInternal:

  1. internal static WindowsIdentity GetCurrentInternal(TokenAccessLevels desiredAccess,bool threadOnly)
  2. {
  3. int errorCode = 0;
  4. bool flag;
  5. SafeTokenHandle currentToken = WindowsIdentity.GetCurrentToken(desiredAccess,threadOnly,out flag,out errorCode);
  6. if (currentToken != null && !currentToken.IsInvalid)
  7. {
  8. WindowsIdentity windowsIdentity = new WindowsIdentity();
  9. windowsIdentity.m_safeTokenHandle.Dispose();
  10. windowsIdentity.m_safeTokenHandle = currentToken;
  11. return windowsIdentity;
  12. }
  13. if (threadOnly && !flag)
  14. {
  15. return null;
  16. }
  17. throw new SecurityException(Win32Native.GetMessage(errorCode));
  18. }

因为从GetCurrent调用时ThreadOnly总是为false,而且currentToken必须对另一个return语句有效,我不认为你有获得一个空的WindowsIdentity的风险.

猜你在找的Windows相关文章