0

Win フォームを使用して PC からリモート サーバーにファイルをアップロードしようとすると、次のエラーが表示されます。私のコンピューターでは、ドメインユーザーを使用しており、リモートサーバーのローカルユーザーのファイルをアップロードしています

ユーザーを偽装する必要があることを発見しましたが、NetworkCredential を偽装する方法がまだわかりませんでした。

この私のコード:

if (tbUsername.Text != string.Empty && tbPassword.Text != string.Empty && userSelectedFilePath != string.Empty)
{
    try
    {
        using (WindowsIdentity.GetCurrent().Impersonate())
        {
            WebClient client = new WebClient();

            NetworkCredential nc = new NetworkCredential("\\\\" + targetServer.Host + "\\" + tbUsername.ToString(), tbPassword.ToString());

            client.Credentials = nc;
            client.UploadFile(targetServer, filepath);
            MessageBox.Show("the file was successfully uploaded", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
else
{
    MessageBox.Show("One of the fields is empty", "Fields Empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
4

2 に答える 2

0

次の手順を実行します。

「コントロールパネル」を開く

「管理ツール」を選択します

「ローカル セキュリティ ポリシー」を開く

左側のペインで、[セキュリティ設定] => [ローカル ポリシー] => [セキュリティ オプション] に移動します。

右側のペインで、[ネットワーク アクセス: ローカル アカウントの共有とセキュリティ モデル] を見つけます。

変更するには、それをダブルクリックします

「クラシック - ローカル ユーザーが自分自身として認証する」に設定します。

于 2015-02-22T11:41:23.953 に答える
0

コード セクションを書き直したところ、問題なく動作するようになりました。次のようになります。

                    IntPtr admin_token = default(IntPtr);
                    WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
                    WindowsIdentity wid_admin = null;
                    WindowsImpersonationContext wic = null;

                    if ((LogonUser(tbUsername.Text, targetServer.Host, tbPassword.Text, 9, 0, ref admin_token)) != 0 || (LogonUser(tbUsername2.Text, targetServer.Host, tbPassword2.Text, 9, 0, ref admin_token)) != 0)
                    {
                        wid_admin = new WindowsIdentity(admin_token);
                        wic = wid_admin.Impersonate();
                    }
于 2015-02-24T07:15:15.140 に答える