zoukankan      html  css  js  c++  java
  • SPS中模拟管理员登陆

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

    ===========================================应用===================================================================
       WindowsImpersonationContext wic 
    = null;
       
    try
       {
        wic 
    = CreateIdentity (Uid,Domain,Pwd).Impersonate();
        
        
    /*do something*/
        
    /*do something*/
       }
       
    catch(Exception ex)
       {
       }
       
    finally
       {
        
    if(wic != null) wic.Undo();
       }
    ===============================================================================================================

      
    #region 虚拟管理员用户
      
    public 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);

      
    #endregion 虚拟管理员用户

  • 相关阅读:
    SQL Server的Execute As与连接池结合使用的测试
    为什么SQL语句Where 1=1 and在SQL Server中不影响性能
    [转]NGINX下配置CACHE-CONTROL
    ls列出当前目录包含子目录下面的所有文件的绝对路径
    [转]无法滚动到溢出容器的Flex项的顶部
    align-items和align-content的区别
    go实现快速排序
    [转]linux超级服务器inetd详解
    makefile 小记
    [转]gcc
  • 原文地址:https://www.cnblogs.com/focus/p/629652.html
Copyright © 2011-2022 走看看