zoukankan      html  css  js  c++  java
  • 共享文件夹(局域网)报错: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

  • 相关阅读:
    易宝支付Demo,生产中封装成简洁的代付接口,不用request如何获取项目运行时的真实路径(转)
    java之IO流的关闭
    Java IO包装流如何关闭?
    qt5.9模块
    九款免费轻量的 AutoCAD 的开源替代品推荐
    QT pro 添加带空格的路径以及添加库文件的正确方法
    QT添加openssl的方法
    手机芯战!麒麟与骁龙上演难分胜负的技术竞速赛(2013以后,芯片和基带都集成到一起去了)
    使用redis缓存加索引处理数据库百万级并发
    TF.Learn
  • 原文地址:https://www.cnblogs.com/tangge/p/12740380.html
Copyright © 2011-2022 走看看