熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

如何扮演另一個帳號(C#實現)

2013-11-13 10:05:13  來源: .NET編程 
如何扮演另一個帳號(C#實現)
 

  using directives#region Using directives

  using System;
using SystemSecurityPrincipal;
using SystemRuntimeInteropServices;
using SystemTextRegularExpressions;
#endregion

  namespace Impersonal
{
    class Program
    {
        測試代碼#region 測試代碼
        public static void Main(string[] args)
        {
            ConsoleWriteLine(當前用戶是:
    + WindowsIdentityGetCurrent()Name);
            ImpersonatedWork testDel = new ImpersonatedWork(Test);
            ImpersonateAndDo(epro\\liping testDel);
            ConsoleWriteLine(當前用戶是:
    + WindowsIdentityGetCurrent()Name);
        }
        static void Test()
        {
            ConsoleWriteLine(當前用戶是:
                + WindowsIdentityGetCurrent()Name);
        }
        #endregion

  [DllImport(advapidll SetLastError = true)]
        public extern static bool LogonUser(String lpszUsername String lpszDomain
            String lpszPassword int dwLogonType
            int dwLogonProvider ref IntPtr phToken);

  [DllImport(kerneldll CharSet = CharSetAuto)]
        public extern static bool CloseHandle(IntPtr handle);

  [DllImport(advapidll CharSet = CharSetAuto SetLastError = true)]
        public extern static bool DuplicateToken(IntPtr ExistingTokenHandle
            int SECURITY_IMPERSONATION_LEVEL ref IntPtr DuplicateTokenHandle);

  public delegate void ImpersonatedWork();
        /**//// <summary>
        /// 以指定用戶的身份去做一件事情
        /// </summary>
        /// <param name=UserName></param>
        /// <param name=PWD></param>
        /// <param name=WhatToDo></param>
        public static void ImpersonateAndDo(string UserName string PWD ImpersonatedWork WhatToDo)
        {
            扮演用戶#region 扮演用戶
            string domainName = stringEmpty;
            string userName = stringEmpty;
            IntPtr tokenHandle = new IntPtr();
            IntPtr dupeTokenHandle = new IntPtr();
            const int LOGON_PROVIDER_DEFAULT = ;
            const int LOGON_LOGON_INTERACTIVE = ;
            const int SecurityImpersonation = ;
            if (! RegexIsMatch(UserName @^\w+[\\]?\w+$))
            {
                throw new ApplicationException(非法的用戶名);
            }
            string[] tmp = UserNameSplit(new char[] { \\ });
            if (tmpLength > )
            {
                domainName = tmp[];
                userName = tmp[];
            }
            else
            {
                userName = tmp[];
            }
            tokenHandle = IntPtrZero;
            dupeTokenHandle = IntPtrZero;
            bool returnValue = LogonUser(userNamedomainName  PWD
                LOGON_LOGON_INTERACTIVE LOGON_PROVIDER_DEFAULT
                ref tokenHandle);
            if (!returnValue)
            {
                throw new ApplicationException(取Handle出錯了!);
            }

  //ConsoleWriteLine(當前用戶是:
            //    + WindowsIdentityGetCurrent()Name);

  bool retVal = DuplicateToken(tokenHandle SecurityImpersonation ref dupeTokenHandle);
            if (!retVal)
            {
                CloseHandle(tokenHandle);
                throw new ApplicationException(復制Handle出錯了!);
            }
            WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle);
            WindowsImpersonationContext impersonatedUser = newIdImpersonate();
            #endregion
            以新用戶身份調用#region 以新用戶身份調用
            WhatToDo();
            #endregion
            取消扮演#region 取消扮演
            impersonatedUserUndo();
            if (tokenHandle != IntPtrZero)
                CloseHandle(tokenHandle);
            if (dupeTokenHandle != IntPtrZero)
                CloseHandle(dupeTokenHandle);
            #endregion
        }
    }
}


From:http://tw.wingwit.com/Article/program/net/201311/12516.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.