winform 操作远程服务器指定的文件读写到本地问题 winform数据库 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 // logon types const int LOGON32_LOGON_INTERACTIVE = 2; const int LOGON32_LOGON_NETWORK = 3; const int LOGON32_LOGON_NEW_CREDENTIALS = 9; // logon providers const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_PROVIDER_WINNT50 = 3; const int LOGON32_PROVIDER_WINNT40 = 2; const int LOGON32_PROVIDER_WINNT35 = 1; [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool RevertToSelf(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern bool CloseHandle(IntPtr handle); private WindowsImpersonationContext impersonationContext; public bool impersonateValidUser(String userName, String domain, String password) { WindowsIdentity tempWindowsIdentity; IntPtr token = IntPtr.Zero; IntPtr tokenDuplicate = IntPtr.Zero; if (RevertToSelf()) { // 这里使用LOGON32_LOGON_NEW_CREDENTIALS来访问远程资源。 // 如果要(通过模拟用户获得权限)实现服务器程序,访问本地授权数据库可 // 以用LOGON32_LOGON_INTERACTIVE if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token) != 0) { if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) { tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); impersonationContext = tempWindowsIdentity.Impersonate(); if (impersonationContext != null) { System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); IPrincipal pr = System.Threading.Thread.CurrentPrincipal; IIdentity id = pr.Identity; CloseHandle(token); CloseHandle(tokenDuplicate); return true; } } } } if (token != IntPtr.Zero) CloseHandle(token); if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate); return false; } public void undoImpersonation() { impersonationContext.Undo(); } public void TestFunc() { bool isImpersonated = false; try { if (impersonateValidUser("服务器登陆名", "Domain", "服务器密码")) { isImpersonated = true; FileStream fs = new FileStream(@"\服务器ip\emrdate\html\test.HTML", FileMode.OpenOrCreate, FileAccess.Read); // FileStream fs = new FileStream(urlsip, FileMode.OpenOrCreate, FileAccess.Read); Encoding encode = System.Text.Encoding.GetEncoding("GBK"); StreamReader myStreamReader = new StreamReader(fs, encode); string strhtml = myStreamReader.ReadToEnd(); string stroutput = StripHTML(strhtml);-- StripHTML()去标签方法 this.textBox1.Text = stroutput; } } finally { if (isImpersonated) undoImpersonation(); } }从网上找这个方法稍作如上修改,就解决了我的问题。 一段扫描端口的代码,希望高人帮我改一个小bug 连excel提示找不到可安装的 ISAM。 为什么断点生成的10个随机数和非断点不同? 从DataGrid到GridView遇到的问题?在线急等。。。。 如何把数据库里的datetime在文本框显示为yyyy-MM-dd格式? 关于ORACLEA数据库连接的问题 一个简单问题,却难了很多人,高手也可以来说说 23号-22号=0? c#.net下将SQL Server数据库文件导出为.txt格式 怎样学习? C#连接oracle 服務中的線程問題!!!!!(急)100分
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
// logon providers
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1; [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int LogonUser(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle); private WindowsImpersonationContext impersonationContext; public bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero; if (RevertToSelf())
{
// 这里使用LOGON32_LOGON_NEW_CREDENTIALS来访问远程资源。
// 如果要(通过模拟用户获得权限)实现服务器程序,访问本地授权数据库可
// 以用LOGON32_LOGON_INTERACTIVE
if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
IPrincipal pr = System.Threading.Thread.CurrentPrincipal;
IIdentity id = pr.Identity;
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
} if (token != IntPtr.Zero)
CloseHandle(token); if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate); return false;
} public void undoImpersonation()
{
impersonationContext.Undo();
} public void TestFunc()
{
bool isImpersonated = false;
try
{
if (impersonateValidUser("服务器登陆名", "Domain", "服务器密码"))
{
isImpersonated = true;
FileStream fs = new FileStream(@"\服务器ip\emrdate\html\test.HTML", FileMode.OpenOrCreate, FileAccess.Read);
// FileStream fs = new FileStream(urlsip, FileMode.OpenOrCreate, FileAccess.Read);
Encoding encode = System.Text.Encoding.GetEncoding("GBK");
StreamReader myStreamReader = new StreamReader(fs, encode);
string strhtml = myStreamReader.ReadToEnd();
string stroutput = StripHTML(strhtml);-- StripHTML()去标签方法
this.textBox1.Text = stroutput;
}
}
finally
{
if (isImpersonated)
undoImpersonation();
}
}
从网上找这个方法稍作如上修改,就解决了我的问题。