简体   繁体   English

C#Win表登录失败

[英]C# Win Form login failure

I'm trying to upload file from my PC to remote server with win form and I get the following error: Logon failure: unknown user name or bad password. 我正在尝试使用win form将文件从我的PC上传到远程服务器,我收到以下错误:登录失败:未知的用户名或密码错误。 On my computer I'm using my domain user and to upload the file local user of the remote server 在我的计算机上,我正在使用我的域用户并上传远程服务器的文件本地用户

I founded, that I need to impersonate my user but I still didn't get how I impersonate NetworkCredential. 我创建,我需要模仿我的用户,但我仍然没有得到我冒充NetworkCredential的方式。

This my code: 这是我的代码:

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);
}

Do following steps: 请执行以下步骤:

Open 'Control panel' 打开'控制面板'

Select 'Administrative Tools' 选择“管理工具”

Open 'Local security policy' 打开“本地安全政策”

On the left pane navigate to 'Security Settings' => 'Local policies' => 'Security Options' 在左侧窗格中,导航至“安全设置”=>“本地政策”=>“安全选项”

On the right pane find 'Network access: Sharing and security model for local accounts' 在右侧窗格中,找到“网络访问:本地帐户的共享和安全模型”

Double-click on it in order to change 双击它以进行更改

Set it to 'Classic - Local users authenticate as themselves' 将其设置为“经典 - 本地用户身份验证为自己”

I have rewritten my code section and it works fine now. 我已经重写了我的代码部分,现在工作正常。 It looks now like this: 现在看起来像这样:

                    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM