繁体   English   中英

使用模拟时获得真实的登录用户

[英]Get real logged in user when impersonation is used

这是一个很难研究的问题...使用模拟时,我如何获得登录的“真实”用户的名称? 我知道有可能我先关闭模拟然后再重新打开,但这在我的情况下很困难,并且正在寻找更简单的方法。

我尝试了以下内容,所有这些都指向ImpersonationUser: Environment.UserName WindowsIdentity.GetCurrent().Name SystemInformation.Username

这是模拟用户的代码(不确定它的帮助程度):

        if (LogonUser(userName, "ABDATA", pwd, LogonSessionType.Interactive, LogonProvider.Default, ref this._userToken))
        {
            if (DuplicateToken(this._userToken, SecurityImpersonationLevel.SecurityImpersonation, ref this._impersonationToken))
            {
                this._identity = new WindowsIdentity(this._impersonationToken, "Kerberos", WindowsAccountType.Normal, true);
                this._impersonationContext = this._identity.Impersonate();
                System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(this._identity);
            }
            else
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }
        }

我们基本上需要该模拟用户的权限,以便它可以访问某些文件,但希望出于责任制目的而能够显示实际登录用户的名称。

真的在寻找任何解决方案..谢谢。

将用户详细信息缓存在ThreadStatic字段中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM