在一些應用情境中,需要以較高的帳戶許可權來執行一段程式從而實現某個功能。比如:在ASP.NET程式中,如果需要寫入一段日誌到檔案中,但空間供應商並沒有給指定目錄asp.net帳戶寫入權限,就可以用到此方法了,前提是你要知道他們給你開的使用者及密碼,一般都是FTP帳號及密碼。當然,最簡單的辦法就是找空間商開通此許可權。上面的情況可能不是必須這樣做的,但以編程方法實現身份類比的方法還是很有的!
代碼如下:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace FCMS.Framework.Utility
{
/// <summary>
/// Windows身份類比。
/// </summary>
public class IdentityAnalogue {
//類比指定使用者時使用的常量定義
/**//// <summary>
///
/// </summary>
public const int LOGON32_LOGON_INTERACTIVE = 2;
/**//// <summary>
///
/// </summary>
public const int LOGON32_PROVIDER_DEFAULT = 0;
/**//// <summary>
///
/// </summary>
WindowsImpersonationContext impersonationContext;
//win32api引用
/**//// <summary>
///
/// </summary>
/// <param name="lpszUserName"></param>
/// <param name="lpszDomain"></param>
/// <param name="lpszPassword"></param>
/// <param name="dwLogonType"></param>
/// <param name="dwLogonProvider"></param>
/// <param name="phToken"></param>
/// <returns></returns>
[DllImport("advapi32.dll")]
public static extern int LogonUserA(string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
/**//// <summary>
///
/// </summary>
/// <param name="hToken"></param>
/// <param name="impersonationLevel"></param>
/// <param name="hNewToken"></param>
/// <returns></returns>
[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
/**//// <summary>
///
/// </summary>
/// <returns></returns>
[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]
public static extern bool RevertToSelf();
/**//// <summary>
///
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
/**//// <summary>
///
/// </summary>
public IdentityAnalogue() {
}
//類比指定的使用者身份
/**//// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="domain"></param>
/// <param name="password"></param>
/// <returns></returns>
public bool ImpersonateValidUser(string userName,string domain,string password) {
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(RevertToSelf()) {
if(LogonUserA(userName,domain,password,2,0,ref token)!=0) {
if(DuplicateToken(token,2,ref tokenDuplicate)!=0) {
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if(impersonationContext!=null) {
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
//取消類比
/**//// <summary>
///
/// </summary>
public void UndoImpersonation() {
impersonationContext.Undo();
}
}
}
使用方法:
//在你執行特定功能的代碼前先驗證類比帳戶:
FCMS.Framework.Utility.IdentityAnalogue ia = new FCMS.Framework.Utility.IdentityAnalogue();
if( ia.ImpersonateValidUser( userName , domain , password ) ){
//執行特定功能的程式碼片段,參數domain可為空白""
}
//最後將使用者上下文恢複為當前表示的Windows使用者
ia.UndoImpersonation();