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

ReSharper警告我可能的NullReferenceException
WindowsIdentity windowsIdentity = new WindowsIdentity(WindowsIdentity.GetCurrent().Token);

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

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

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

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

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...