첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

TestProject.zip
다운로드

▶ Program.cs

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

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 사용자 로그온 하기 - LogonUser(userAccount, domain, password, logonType, logonProvider, tokenHandle)

        /// <summary>
        /// 사용자 로그온 하기
        /// </summary>
        /// <param name="userAccount">사용자 계정</param>
        /// <param name="domain">도메인</param>
        /// <param name="password">패스워드</param>
        /// <param name="logonType">로그온 타입</param>
        /// <param name="logonProvider">로그온 제공자</param>
        /// <param name="tokenHandle">토큰 핸들</param>
        /// <returns>처리 결과</returns>
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LogonUser(string userAccount, string domain, string password, int logonType, int logonProvider, ref IntPtr tokenHandle);

        #endregion
        #region 핸들 닫기 - CloseHandle(tokenHandle)

        /// <summary>
        /// 핸들 닫기
        /// </summary>
        /// <param name="tokenHandle">토큰 핸들</param>
        /// <returns>처리 결과</returns>
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private extern static bool CloseHandle(IntPtr tokenHandle);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

            Console.WriteLine("현재 윈도우즈 신원");
            Console.WriteLine("----------------------------------------"     );
            Console.WriteLine("사용자 계정 : {0}", windowsIdentity.Name      );
            Console.WriteLine("사용자 SDDL : {0}", windowsIdentity.User.Value);

            string userAccount = "test";
            string domain      = "";
            string password    = "1234";

            IntPtr tokenHandle = IntPtr.Zero;

            try
            {
                if(LogonUser(userAccount, domain, password, 3, 0, ref tokenHandle))
                {
                    using(WindowsIdentity newWindowsIdentity = new WindowsIdentity(tokenHandle))
                    {
                        Console.WriteLine("");
                        Console.WriteLine("사용자 계정을 변경합니다.");

                        using(WindowsImpersonationContext windowsImpersonationContext = newWindowsIdentity.Impersonate())
                        {
                            windowsIdentity = WindowsIdentity.GetCurrent();

                            Console.WriteLine("현재 윈도우즈 신원");
                            Console.WriteLine("----------------------------------------"     );
                            Console.WriteLine("사용자 계정 : {0}", windowsIdentity.Name      );
                            Console.WriteLine("사용자 SDDL : {0}", windowsIdentity.User.Value);

                            Console.WriteLine("");
                            Console.WriteLine("사용자 계정 변경을 취소합니다.");

                            windowsImpersonationContext.Undo();

                            windowsIdentity = WindowsIdentity.GetCurrent();

                            Console.WriteLine("현재 윈도우즈 신원");
                            Console.WriteLine("----------------------------------------"     );
                            Console.WriteLine("사용자 계정 : {0}", windowsIdentity.Name      );
                            Console.WriteLine("사용자 SDDL : {0}", windowsIdentity.User.Value);
                        }
                    }
                }
            }
            finally
            {
                if(tokenHandle != IntPtr.Zero)
                {
                    CloseHandle(tokenHandle);
                }
            }
        }

        #endregion
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,