模拟登陆os
2008-05-15 22:14
288 查看
由于IIS的服务帐户往往没有权限,IO操作比较容易出问题,采用模拟登录
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Linq;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Xml.Linq;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Security.Principal;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class _Default : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int LOGON32_LOGON_INTERACTIVE = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int LOGON32_PROVIDER_DEFAULT = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WindowsImpersonationContext impersonationContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int LogonUserA(String lpszUserName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String lpszDomain,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String lpszPassword,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dwLogonType,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dwLogonProvider,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref IntPtr phToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int DuplicateToken(IntPtr hToken,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int impersonationLevel,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref IntPtr hNewToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern bool RevertToSelf();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern bool CloseHandle(IntPtr handle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (impersonateValidUser("test1", "contoso.local", "password01!"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Insert your code that runs under the security context of a specific user here.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//button1.Text = "ok";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] att = File.ReadAllBytes(@"/fileserver est1 est.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string contentType = "application/octet-stream";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string filename = "test.txt";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.AppendHeader("Content-Disposition", "attachment;FileName=" + filename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.AppendHeader("Content-Length", att.Length.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.BinaryWrite(att);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
undoImpersonation();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private bool impersonateValidUser(String userName, String domain, String password)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WindowsIdentity tempWindowsIdentity;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr token = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr tokenDuplicate = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (RevertToSelf())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
impersonationContext = tempWindowsIdentity.Impersonate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (impersonationContext != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(token);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (token != IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(token);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (tokenDuplicate != IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void undoImpersonation()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
impersonationContext.Undo();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Linq;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Xml.Linq;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Security.Principal;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class _Default : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int LOGON32_LOGON_INTERACTIVE = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int LOGON32_PROVIDER_DEFAULT = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WindowsImpersonationContext impersonationContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int LogonUserA(String lpszUserName,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String lpszDomain,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String lpszPassword,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dwLogonType,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int dwLogonProvider,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref IntPtr phToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern int DuplicateToken(IntPtr hToken,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int impersonationLevel,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref IntPtr hNewToken);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern bool RevertToSelf();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static extern bool CloseHandle(IntPtr handle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (impersonateValidUser("test1", "contoso.local", "password01!"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Insert your code that runs under the security context of a specific user here.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//button1.Text = "ok";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] att = File.ReadAllBytes(@"/fileserver est1 est.txt");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string contentType = "application/octet-stream";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string filename = "test.txt";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.AppendHeader("Content-Disposition", "attachment;FileName=" + filename);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.AppendHeader("Content-Length", att.Length.ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.BinaryWrite(att);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Response.Flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
undoImpersonation();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private bool impersonateValidUser(String userName, String domain, String password)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WindowsIdentity tempWindowsIdentity;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr token = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr tokenDuplicate = IntPtr.Zero;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (RevertToSelf())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
impersonationContext = tempWindowsIdentity.Impersonate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (impersonationContext != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(token);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (token != IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(token);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (tokenDuplicate != IntPtr.Zero)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle(tokenDuplicate);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void undoImpersonation()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
impersonationContext.Undo();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 模拟登陆115网盘(MFC版)
- python模拟网站登陆
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
- Scrapy中用cookie模拟登陆新浪微博
- 【教程】模拟登陆百度之Java代码版
- php模拟登陆
- SharePoint 自定义登录页面代码(SharePoint 模拟登陆)
- 模拟网站登陆
- PHP模拟登陆
- 模拟登陆豆瓣获取cookie入mongo库!
- c#模拟网页实现12306登陆、自动刷票、自动抢票完全篇
- 使用C#的HttpWebRequest模拟登陆网站
- [Scrapy]模拟登陆并获取Cookie值
- 使用C#的HttpWebRequest模拟登陆网站(续)
- 使用C#的HttpWebRequest模拟登陆网站
- 通过HttpWebRequest实现模拟登陆
- php的curl扩展抓取信息——模拟登陆成功却无法抓取页面等问题
- 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆
- python3 模拟登陆微博二