728x90
728x170
▶ 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
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] 런타임에서 코드로 C# 코드 생성하기 (0) | 2018.04.26 |
---|---|
[C#/COMMON] CodeDomProvider 클래스 : 런타임에서 C# 코드를 동적으로 컴파일하고 DLL 파일 생성하기 (0) | 2018.04.26 |
[C#/COMMON] CSharpCompilation 클래스 : 런타임에서 C# 코드를 동적으로 컴파일하기 (0) | 2018.04.26 |
[C#/COMMON] Stream 클래스 : 스로틀(Throttle) 스트림 만들기 (0) | 2018.04.09 |
[C#/COMMON] 특정 파일을 사용하는 프로세스 리스트 구하기 (0) | 2018.03.29 |
[C#/COMMON] 콘솔(Console) 닫기 버튼 비활성화 하기 (0) | 2018.03.22 |
[C#/COMMON] 디버그 모드에서 프로세스 참조 구하기 (0) | 2018.03.15 |
[C#/COMMON] ITypedList 인터페이스 : TypedCollection<T> 만들기 (0) | 2018.03.11 |
[C#/COMMON] BitConverter : ToString 정적 메소드를 사용해 바이트 배열에서 문자열 구하기 (0) | 2018.03.04 |
[C#/COMMON] FileAttributes 클래스 : 디렉토리 여부 구하기 (0) | 2018.03.04 |