码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-20 21:35:38      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ogg   mac   try   stat   ast   VID   files   new   extra   

我用 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

标签:ogg   mac   try   stat   ast   VID   files   new   extra   

原文地址:https://www.cnblogs.com/tangge/p/12740380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!