共享文件夹(局域网)报错:The username or password is incorrect
我用 Console APP 可以访问创建,但是我们的Web程序就报错:
The username or password is incorrect
也是奇葩了。找到一位大神的方法:
——————————————————————————————————————————————————————————————————
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
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/p/12740380.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具