asp.net(C#)中实现身份模拟(转贴)
来源:互联网 发布:淘宝收藏互刷 编辑:程序博客网 时间:2024/05/02 11:49
asp.net(C#)中实现身份模拟
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;
{
///
/// Windows身份模拟。
///
public class IdentityAnalogue {
//模拟指定用户时使用的常量定义
/**////
///
///
public const int LOGON32_LOGON_INTERACTIVE = 2;
/**////
///
///
public const int LOGON32_PROVIDER_DEFAULT = 0;
///
///
WindowsImpersonationContext impersonationContext;
/**//// mmary>
///
///
///
///
///
///
///
///
///
[DllImport("advapi32.dll")]
public static extern int LogonUserA(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);
/**////
///
///
public IdentityAnalogue() {
}
//模拟指定的用户身份
/**////
///
///
///
///
/// ram>
///
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;
}
/**////
///
///
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();
- asp.net(C#)中实现身份模拟(转贴)
- ASP.NET实现身份模拟
- ASP.NET实现身份模拟
- ASP.NET实现身份模拟
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- asp.net身份模拟
- ASP.NET 模拟身份
- 在ASP.NET应用程序中使用身份模拟 impersonate
- 实例在ASP.NET应用程序中使用身份模拟
- 在ASP.Net中模拟新的用户身份
- 在ASP.NET应用程序中使用身份模拟(Impersonation)
- HASH分区表增加新的分区的一点研究
- 条形码限制手工输入的一种方法。
- 在BREW中打造自己的GUI(2)-TabPane的实现
- Eclipse启动不了解决方法
- 老程序员十年生涯黯然总结
- asp.net(C#)中实现身份模拟(转贴)
- 计算Oracle表空间的大小 利用率
- web中调用outlook
- 在JSP页面中自定义标签
- ASP.NET程序中常用的三十三种代码
- AIX 日常维护
- 在BREW中打造自己的GUI(3)-做一个跑马灯的效果
- asp.net中的弹出对话框(舟无泊总结)
- TweenLite, Flash渐变第三方模块