一時的にドメインユーザーアカウントを偽装して、ASP.NETサイトからネットワークドライブ上のファイルを読み取ることができます。一時的にユーザーを偽装してファイルを開くにはどうすればよいですか?
サイト全体に偽装を設定したり、サーバー上にマップされたドライブを設定したりするのではなく、
一時的にドメインユーザーアカウントを偽装して、ASP.NETサイトからネットワークドライブ上のファイルを読み取ることができます。一時的にユーザーを偽装してファイルを開くにはどうすればよいですか?
サイト全体に偽装を設定したり、サーバー上にマップされたドライブを設定したりするのではなく、
私はMichiel van Otegem: WindowsImpersonationContext made easyのコードを使用して終了し、IDisposableの実装を追加しました。私はこれをanother question about impersonation in ASP.NETで見つけました。
使用法:
using (WindowsImpersonationContextFacade impersonationContext
= new WindowsImpersonationContextFacade(
Settings.Default.ImpersonationDomain,
Settings.Default.ImpersonationUser,
Settings.Default.ImpersonationPass))
{
transactions = TransactionLoader.Load(filePath);
}
コード:
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.ComponentModel;
namespace MyNamespace
{
public class WindowsImpersonationContextFacade : IDisposable
{
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType, int dwLogonProvider,
ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
private const int LOGON32_PROVIDER_DEFAULT = 0;
private const int LOGON32_LOGON_INTERACTIVE = 2;
private string m_Domain;
private string m_Password;
private string m_Username;
private IntPtr m_Token;
private WindowsImpersonationContext m_Context = null;
protected bool IsInContext
{
get { return m_Context != null; }
}
public WindowsImpersonationContextFacade(string domain, string username, string password)
{
m_Domain = domain;
m_Username = username;
m_Password = password;
Enter();
}
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
private void Enter()
{
if (this.IsInContext) return;
m_Token = IntPtr.Zero;
bool logonSuccessfull = LogonUser(
m_Username,
m_Domain,
m_Password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref m_Token);
if (logonSuccessfull == false)
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}
WindowsIdentity identity = new WindowsIdentity(m_Token);
m_Context = identity.Impersonate();
}
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
private void Leave()
{
if (this.IsInContext == false) return;
m_Context.Undo();
if (m_Token != IntPtr.Zero) CloseHandle(m_Token);
m_Context = null;
}
public void Dispose()
{
Leave();
}
}
}
実は、プロセスは非常に簡単です、あなたはこのようなコードを使用することができます。
using System.Security.Principal;
...
// Obtain the authenticated user's Identity
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
// Start impersonating
ctx = winId.Impersonate();
// Now impersonating
// Access resources using the identity of the authenticated user
}
// Prevent exceptions from propagating
catch
{
}
finally
{
// Revert impersonation
if (ctx != null)
ctx.Undo();
}
// Back to running under the default ASP.NET process identity
あなたのコードは「今すぐ偽装」セクションに入ります。 KEYはfinallyブロックですが、これは非常に重要です。この方法の詳細については、MSDNの記事をご覧ください。
これは、ドメイン、ユーザー名、パスワードを渡すことによっても使用できますか? –
はい、新しいWindows IDを作成するだけです。 –
個人的には、dllimportsやその他のアイテムがあるので、私はこれのファンではないでしょうし、全体的にあなたはそれをやることなく得ることができます。 –