在ASP.NET应用程序中使用身份模拟(Impersonation)

MSDN资源
在ASP.NET应用程序中使用身份模拟(Impersonation)
地址:
 
其他资源
using  System;
using  System.Security.Principal;
using  System.Security.Permissions;
using  System.Runtime.InteropServices;

namespace  CommonLibrary
{
 
/// <summary>
 
/// 模拟其他用户帐号
 
/// </summary>

 public class UserIdentity
 
{
  
public static WindowsImpersonationContext wic = null;
//  public static void ImpersonateAdmin()
//  {
//   wic = CreateIdentity(BSConfig.User(),BSConfig.Domain(),BSConfig.Password()).Impersonate();
//  }
  /// <summary>
  
/// 身份摸拟
  
/// </summary>
  
/// <param name="User">Name</param>
  
/// <param name="Domain">Domain</param>
  
/// <param name="Password">Password</param>

  public static void ImpersonateAdmin(string User, string Domain, string Password)
  
{
   wic 
= CreateIdentity(User, Domain, Password).Impersonate();
  }

  
public static void EndImpersonate()
  
{
   wic.Undo();
  }

  
protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
  
{
   
// The Windows NT user token.
   IntPtr tokenHandle = new IntPtr(0);
   
const int LOGON32_PROVIDER_DEFAULT = 0;
   
const int LOGON32_LOGON_NETWORK = 3;
   tokenHandle 
= IntPtr.Zero;
   
// Call LogonUser to obtain a handle to an access token.
   bool returnValue = LogonUser(User, Domain, Password,LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,ref tokenHandle);
   
if (false == returnValue)
   
{
    
int ret = Marshal.GetLastWin32Error();
    
throw new Exception("LogonUser failed with error code: " +  ret);
   }

   System.Diagnostics.Debug.WriteLine(
"Created user token: " + tokenHandle);
   
//The WindowsIdentity class makes a new copy of the token.
   
//It also handles calling CloseHandle for the copy.
   WindowsIdentity id = new WindowsIdentity(tokenHandle);
   CloseHandle(tokenHandle);
   
return id;
  }

  [DllImport(
"advapi32.dll", SetLastError=true)]
  
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  [DllImport(
"kernel32.dll", CharSet=CharSet.Auto)]
  
private extern static bool CloseHandle(IntPtr handle);
 }

}

用类似于如下的方式调用以上类
     string  imPersonUser  =  ConfigurationSettings.AppSettings.Get( " ImPersonUser " );
    
string  imPersonDomain  =  ConfigurationSettings.AppSettings.Get( " ImPersonDomain " );
    
string  imPersonPassword  =  ConfigurationSettings.AppSettings.Get( " ImPersonPassword " );
    DESCryptoServiceProvider des 
=   new  DESCryptoServiceProvider();
    imPersonPassword 
=  des.Dencrypt(imPersonPassword);
    UserIdentity.ImpersonateAdmin(imPersonUser,imPersonDomain,imPersonPassword); 
    .......
 
    UserIdentity.EndImpersonate();
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值