[英]C# Win Form login failure
我正在嘗試使用win form將文件從我的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);
}
請執行以下步驟:
打開'控制面板'
選擇“管理工具”
打開“本地安全政策”
在左側窗格中,導航至“安全設置”=>“本地政策”=>“安全選項”
在右側窗格中,找到“網絡訪問:本地帳戶的共享和安全模型”
雙擊它以進行更改
將其設置為“經典 - 本地用戶身份驗證為自己”
我已經重寫了我的代碼部分,現在工作正常。 現在看起來像這樣:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.