Imprusiate = trueを使用するときにIISで現在のアプリケーションプールユーザーを取得する方法は?
-
02-10-2019 - |
質問
.NETでは、IISでホストされているWebサイトが現在のユーザーをどのように取得しますか? IEアプリケーションプールユーザーは、サイトにアクセスしている現在のユーザーではありません。
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;
}
純粋にユーザーを見る必要がある場合は、環境を使用するだけではありませんか?
私は環境を再構成して、クラシックなアプリプール(なりすましをオンにして)で実行するようになりました。ユーザーはIUSRがなりすまして出てきます。
ジョン・サイモンズ:まさに私が欲しかったもの、ありがとう。 VB.NETバージョンの場合:
With System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero)
Dim sCurrentUserName As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name
End With
所属していません StackOverflow