带用户名密码---远程访问另一台主机中共享文件夹中文件
2008-09-01 17:15
806 查看
using System.Runtime.InteropServices;
using System;
using System.ComponentModel;
using System.IO;
enum LogonType : uint
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}
enum LogonProvider : uint
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3,
}
class IdentityScope : IDisposable
{
[DllImport("Advapi32.dll")]
static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
LogonType dwLogonType, LogonProvider dwLogonProvider, out IntPtr phToken);
[DllImport("Advapi32.DLL")]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("Advapi32.DLL")]
static extern bool RevertToSelf();
[DllImport("Kernel32.dll")]
static extern int GetLastError();
bool disposed;
public IdentityScope(string domain, string userName, string password): this(domain, userName, password, LogonType.Interactive, LogonProvider.Default)
{
}
public IdentityScope(string domain, string userName, string password, LogonType logonType, LogonProvider logonProvider)
{
if (string.IsNullOrEmpty(userName))
{
throw new ArgumentNullException("userName");
}
if (string.IsNullOrEmpty(domain))
{
domain = ".";
}
IntPtr token;
int errorCode = 0;
if (LogonUser(userName, domain, password, logonType, logonProvider, out token))
{
if (!ImpersonateLoggedOnUser(token))
{
errorCode = GetLastError();
}
}
else
{
errorCode = GetLastError();
}
if (errorCode != 0)
{
throw new Win32Exception(errorCode);
}
}
~IdentityScope()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Nothing to do.
}
RevertToSelf();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class test
{
//下面是一个测试函数:
public static void aaa()
{
using (new IdentityScope(@"10.0.0.68", "aaa", "qwert", LogonType.NewCredentials, LogonProvider.WinNT50))
{
File.Copy("////10.0.0.68//autotxt//2008-3-24 17.22.28.txt", @"C:/rere.txt");
}
}
}
using System;
using System.ComponentModel;
using System.IO;
enum LogonType : uint
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}
enum LogonProvider : uint
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3,
}
class IdentityScope : IDisposable
{
[DllImport("Advapi32.dll")]
static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
LogonType dwLogonType, LogonProvider dwLogonProvider, out IntPtr phToken);
[DllImport("Advapi32.DLL")]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("Advapi32.DLL")]
static extern bool RevertToSelf();
[DllImport("Kernel32.dll")]
static extern int GetLastError();
bool disposed;
public IdentityScope(string domain, string userName, string password): this(domain, userName, password, LogonType.Interactive, LogonProvider.Default)
{
}
public IdentityScope(string domain, string userName, string password, LogonType logonType, LogonProvider logonProvider)
{
if (string.IsNullOrEmpty(userName))
{
throw new ArgumentNullException("userName");
}
if (string.IsNullOrEmpty(domain))
{
domain = ".";
}
IntPtr token;
int errorCode = 0;
if (LogonUser(userName, domain, password, logonType, logonProvider, out token))
{
if (!ImpersonateLoggedOnUser(token))
{
errorCode = GetLastError();
}
}
else
{
errorCode = GetLastError();
}
if (errorCode != 0)
{
throw new Win32Exception(errorCode);
}
}
~IdentityScope()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Nothing to do.
}
RevertToSelf();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
public class test
{
//下面是一个测试函数:
public static void aaa()
{
using (new IdentityScope(@"10.0.0.68", "aaa", "qwert", LogonType.NewCredentials, LogonProvider.WinNT50))
{
File.Copy("////10.0.0.68//autotxt//2008-3-24 17.22.28.txt", @"C:/rere.txt");
}
}
}
相关文章推荐
- .net使用用户名和密码访问远程共享文件夹的两种方法
- 访问文件共享服务器,显示无访问权限,不提示用户名和密码输入
- java远程共享文件访问jcifs解决密码中特殊符号问题
- 如何取消server2003的共享文件和文件夹使用时的用户名和密码
- C# 访问共享文件夹或者磁盘(需要用户名密码)
- Windows7在访问网络共享文件夹提示“登陆失败:未知的用户名或密码错误”
- win7访问共享文件夹提示“未知的用户名或密码错误”
- 如何取消server2003的共享文件和文件夹使用时的用户名和密码
- 如何更改访问共享文件夹的用户名和密码
- windows系统共享文件夹访问无需验证(输入用户名和密码)即可进入
- MAC通过SMB访问windows共享文件夹:为何用户名密码都正确却无法通过
- [转]访问共享文件夹提示“未知的用户名或密码错误
- 【Linux】虚拟机Ubuntu系统访问主机windows共享文件夹文件类型出错
- 处理win7访问win2003共享文件时在正确的用户名密码下却提示【登录失败:未知的用户名或密码】
- WinXP下用用户名和密码访问Ubuntu共享文件------Samba服务!
- 如何设置共享文件夹访问不需要输入用户名和密码
- 如何让域用户去访问工作组的共享文件而不会弹出认证用户名和密码?
- c++类内强制类型转换&访问共享文件夹提示0未知的用户名或密码错误
- java远程共享文件访问jcifs解决密码中特殊符号问题
- 本地计算机无法识别正在用于访问远程共享文件夹的用户名