[英]How to impersonate another Windows user, when using Windows authentication?
[英]How to get the current application pool user in IIS when using impersonate=true?
在 .net 中,當網站托管在 IIS 中時,您如何獲取網站正在運行的當前用戶。 即應用程序池用戶不是訪問該站點的當前用戶。
使用 windows 集成和模擬。
<authentication mode="Windows"/>
<identity impersonate="true"/>
要在托管代碼中恢復為應用程序池用戶,您可以執行以下操作:
using (WindowsIdentity.Impersonate(IntPtr.Zero))
{
//This code executes under app pool user
}
找到了解決辦法。
使用 RevertToSelf 您可以從線程中去除模擬。 在 IIS 中,這等同於應用程序池用戶。
一些文件
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;
}
如果您純粹需要查看用戶,那么您不能只使用 Environment.UserName 嗎?
我剛剛重新配置了我的環境以使用經典應用程序池運行(啟用模擬),並且用戶作為 IUSR 出現並啟用模擬。
約翰西蒙斯:正是我想要的,謝謝。 對於 VB.net 版本:
With System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero)
Dim sCurrentUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.