我正在尝试使用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);
}
答案 0 :(得分:0)
执行以下步骤:
打开“控制面板”
选择“管理工具”
打开“本地安全政策”
在左侧窗格中,导航至“安全设置”=> '本地政策'=> '安全选项'
在右侧窗格中,找到“网络访问:本地帐户的共享和安全模型”
双击它以便更改
将其设置为“经典 - 本地用户身份验证为自己”
答案 1 :(得分: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();
}