0
このテーマは新しいものではありません。しかし、私は専門家の助けが必要です。 私はローカルシステム(ドメインのコンピュータではありません)上で動作するWindowsフォームアプリケーションを作っています。アプリケーションは、Active Directoryドメインの共有フォルダにフォルダといくつかのファイルを作成します。 私が偽装について読んだことがあると、このようにいくつかのことをするために試してみました:Impersonation by using LogonUserC#ActiveDirectoryと偽装のリモートサーバー上のファイルシステム
それから私は、次のコードを書いた:
using System.Security.Principal;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
private enum LogonSessionType : uint
{
Interactive = 2,
Network,
Batch,
Service,
NetworkCleartext = 8,
NewCredentials
}
private enum LogonProvider : uint
{
Default = 0,
WinNT35,
WinNT40,
WinNT50
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonSessionType logonType,
LogonProvider logonProvider,
out IntPtr token);
public Form1()
{
InitializeComponent();
}
protected void btnConnect_Click(object sender, EventArgs e)
{
IntPtr token = IntPtr.Zero;
WindowsImpersonationContext impersonateUser = null;
try
{
bool result = LogonUser("[email protected]", "192.168.1.1", "[email protected]",
LogonSessionType.Network, LogonProvider.Default, out token);
if(result)
{
WindowsIdentity id = new WindowsIdentity(token);
impersonateUser = id.Impersonate();
string showtext = string.Format("Identity: {0}", WindowsIdentity.GetCurrent().Name);
MessageBox.Show(showtext);
}
else
{
string showtext = string.Format("Identity: {0}", "Fail");
MessageBox.Show(showtext);
}
}
catch
{
}
finally
{
if(impersonateUser!=null)
impersonateUser.Undo();
if (token != IntPtr.Zero)
CloseHandle(token);
}
}
}
しかしブール結果は常に= 偽です。私は間違って何をしていますか?
を、あなたは実際には「[email protected]」の値を渡すか、彼らはあなたがあなたの本当のユーザー名とパスワードを入れないために使用される一例に過ぎていますか? – gabsferreira
LogonUser(...)の後にGetLastError()を呼び出して、Windowsが何を問題としているかを確認します。 – adelphus