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
  • 相关阅读:
    [LeetCode]String主题系列{第5,6题}
    [LeetCode]Math主题系列{第7,9,13,273题}
    [LeetCode]LinkedList主题系列{第2题}
    [LeetCode]HashTable主题系列{第3,36,49,94,136,138,187,202,204,205,290,347,389,409,438,451,463,500,508,560,594,599题}
    由于博客园的道路越来越像CSDN了,已经不再是当年的博客园了,决定退出博客园,后续建立自己的博客站点。
    Revit二次开发——非模态窗口的事件处理
    Revit二开---Schemachema扩展数据
    Revit二开---ViewFamily视图类型枚举
    CAD二开---扩展数据XData
    CAD二开---框选文字批量对齐
  • 原文地址:https://www.cnblogs.com/stswordman/p/1308250.html
Copyright © 2011-2022 走看看