非常に簡単な方法でファイルアップロードツールを使用しています。ファイル (20 ~ 25 KB 程度の非常に小さいファイル) を取得して、リモート サーバーに転送する必要があります。
各サーバーは特定のユーザー名とパスワードへのアクセスを許可しており、File.SaveAs() 関数を使用する前に偽装を使用する必要がありました。
public void btnUploadFile_Click(Object sender, EventArgs e)
{
try
{
string strUploadDir = GetFileUploadLocation(ddlRole.SelectedItem.Value, fuupload.FileName);
objCommon.ImpersonateUser("xxxxxx","xxx", "xxx");
fuupload.SaveAs(strUploadDir + "\\" + fuupload.FileName);
objCommon.EndImpersonation();
divUploadMessage.InnerHtml = "<font color='green'><b>File uploaded successfully.</b></font><br>";
}
catch (Exception ex)
{
divUploadMessage.InnerHtml = "<font color='red'><b>Failed to upload. Please try again. Error : " + ex.ToString() + "</b></font><br>";
}
}
今、私が抱えている問題:
- あるサーバーでファイルをアップロードすると、ファイルサイズがゼロになるため、基本的に空のファイルになります
- これを使用すると、他のサーバーから、不明なユーザー名またはパスワードが正しくないというエラーが表示されますが、ユーザーの資格情報を確認しましたが、問題ありません。さらに、IIS 設定はユーザー Windows 認証に設定されています。
偽装が使用されているものについては、以下のコードです。
public bool ImpersonateUser(String userName, String domain, String password)
{
WindowsIdentity objIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
objIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = objIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}