当使用模糊= true时,如何在IIS中获取当前的应用程序池用户?
-
02-10-2019 - |
题
在.NET中,当托管IIS的网站如何获得当前用户网站正在运行时。即应用程序池用户不是当前访问该站点的当前用户。
使用Windows集成和模仿。
<authentication mode="Windows"/>
<identity impersonate="true"/>
解决方案
要恢复到托管代码中的应用程序池用户,您可以执行以下操作:
using (WindowsIdentity.Impersonate(IntPtr.Zero))
{
//This code executes under app pool user
}
其他提示
找到了解决方案。
使用恢复原状,您可以从线程中剥离模仿。在IIS中,这等同于应用程序池用户。
一些doco
http://www.pinvoke.net/default.aspx/advapi32.reverttoself
http://msdn.microsoft.com/en-us/library/aa379317%28vs.85%29.aspx
和代码
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool RevertToSelf();
private static WindowsIdentity GetAppPoolIdentity()
{
WindowsIdentity identity = null;
Win32Exception win32Exception = null;
var thread = new Thread(o =>
{
if (!RevertToSelf())
{
var win32error = Marshal.GetLastWin32Error();
win32Exception = new Win32Exception(win32error);
}
identity = WindowsIdentity.GetCurrent();
});
thread.Start();
thread.Join();
if (win32Exception != null)
{
throw win32Exception;
}
return identity;
}
如果您纯粹需要查看用户,那么您不能只使用verigustry.username吗?
我只是重新配置了我的环境,以使用经典的应用程序池(带有模仿),用户随着iusr的含义而出现。
约翰·西蒙斯(John Simons):我想要什么,谢谢。对于vb.net版本:
With System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero)
Dim sCurrentUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name
End With
不隶属于 StackOverflow