共享文件夹(局域网)报错:The username or password is incorrect

Posted tangge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了共享文件夹(局域网)报错:The username or password is incorrect相关的知识,希望对你有一定的参考价值。

我用 Console APP 可以访问创建,但是我们的Web程序就报错:

The username or password is incorrect

也是奇葩了。找到一位大神的方法:

https://stackoverflow.com/questions/582988/can-you-explain-why-directoryinfo-getfiles-produces-this-ioexception

——————————————————————————————————————————————————————————————————
I have this same problem when trying to access the file system of a windows server in a different domain. The problem is that the user account that the program is running under does not have access to the remote server. Windows does extra work behind the scenes to make it look seamless when you use Windows Explorer because it guesses that your remote credentials will match your local credentials.

If you map a drive locally to the remote server, then use the locally mapped drive in your code, you shouldn‘t have the problem. If you can‘t map a drive, but you can hard code the credentials to use for the remote server, then you can use this code:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace Company.Security
{
    public class ImpersonateUser : IDisposable
    {
        [DllImport("advapi32.dll", SetLastError=true)]
        private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

        [DllImport( "kernel32", SetLastError = true )]
        private static extern bool CloseHandle(IntPtr hObject);

        private IntPtr userHandle = IntPtr.Zero;
        private WindowsImpersonationContext impersonationContext;

        public ImpersonateUser( string user, string domain, string password )
        {
            if ( ! string.IsNullOrEmpty( user ) )
            {
                // Call LogonUser to get a token for the user
                bool loggedOn = LogonUser( user, domain, password,
                    9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
                    3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
                    out userHandle );
                if ( !loggedOn )
                    throw new Win32Exception( Marshal.GetLastWin32Error() );

                // Begin impersonating the user
                impersonationContext = WindowsIdentity.Impersonate( userHandle );
            }
        }

        public void Dispose()
        {
            if ( userHandle != IntPtr.Zero )
                CloseHandle( userHandle );
            if ( impersonationContext != null )
                impersonationContext.Undo();
        }
    }
}

Then you can access the remote server by doing this:

using ( new ImpersonateUser( "UserID", "Domain", "Password" ) )
{
    // Any IO code within this block will be able to access the remote server.
}

answered Feb 26 ‘09 at 21:00

————————————————————————————————————————
I have a similar method for impersonating a user, but it seems to only work when the user is a local admin. Do you find this to be the case? – flipdoubt Feb 26 ‘09 at 21:12
Which user needs to be a local admin? Your remote user might need to be an admin in order to access the share. – David Feb 27 ‘09 at 18:02
I‘m saying you need to have some kind of "can impersonate user" permission on the local machine, which most standard users don‘t. – flipdoubt Mar 5 ‘09 at 1:53
Also, my problem is intermittent. One moment, the user is accessing the server; the next moment, the user gets this IOException; the next moment, the user is accessing the server. – flipdoubt Mar 5 ‘09 at 1:55
I‘ve made almost same class as ImpersonateUser some time ago, which also was implementing IDisposable to use it inside using block. Happy to see that I‘m not the only one. – okutane Mar 5 ‘09 at 9:02

以上是关于共享文件夹(局域网)报错:The username or password is incorrect的主要内容,如果未能解决你的问题,请参考以下文章

windows server2019共享文件配额报错The quota limit is 10240.00 MB and the current usage is 10213.39 MB (99% o

出现“计算机无法访问,没有权限使用网络资源。请与这台服务器的管理员联系”的报错,如何解决

webpy 访问局域网共享资源

Python一秒搭建ftp服务器,帮助你在局域网共享文件华为云技术分享

启动项目报错:502 Server dropped connection The following error occurred while trying to access http://loc

局域网共享文件夹权限问题