zoukankan      html  css  js  c++  java
  • impersonate c#

    using  System;
    using System.Security.Principal;
    using System.Runtime.InteropServices;

    namespace impersonate
    {
        
    public class ImpersonateConnection
        {

            
    //definition for Impersonation
            public const int LOGON32_LOGON_INTERACTIVE = 2;
            
    public const int LOGON32_PROVIDER_DEFAULT = 0;

            WindowsImpersonationContext impersonationContext;

            [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);
            
    //------------------------------



            
    //impersonate another valid user
            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, LOGON32_LOGON_INTERACTIVE,
                        LOGON32_PROVIDER_DEFAULT, 
    ref token) != 0)
                    {
                        
    if (DuplicateToken(token, 2ref 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;
            }

            
    //unimpersonate validate user
            public void undoImpersonation()
            {
                impersonationContext.Undo();
            }



            
    //end of class
        }
    }



      ImpersonateConnection i 
    = new ImpersonateConnection();
       i.impersonateValidUser(
    "stswordman""domain1 ""p@s5word"));
    //the following code would be run under domain1\stswormdan ‘s context
    //
      i.undoImpersonation();//revert to original context
  • 相关阅读:
    java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
    Java里的构造函数(构造方法)
    Java里this的作用和用法
    JAVA中的重载和重写
    从键盘接收字符类型的数据并实现剪刀石头布的规则
    使用Notepad++编码编译时报错(已解决?)
    云就是网络,云计算呢
    使用JavaMail创建邮件和发送邮件
    mysql锁机制
    java中几种常用的设计模式
  • 原文地址:https://www.cnblogs.com/stswordman/p/1308250.html
Copyright © 2011-2022 走看看