๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/C#

[C# .net] Windows ์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ ๋ฐฉ๋ฒ•

by Jay Din 2024. 12. 11.
728x90
๋ฐ˜์‘ํ˜•

ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ณด์•ˆ ์ธ์ฆ์„ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜, ํŠน์ • ๊ณ„์ •์œผ๋กœ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ํ™œ์šฉํ•  ๋•Œ ์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž๋ฅผ ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€๋ น, ํšŒ์‚ฌ์˜ ํŒŒ์ผ ์„œ๋ฒ„์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ๊ถŒํ•œ์ด ๋ถ€์—ฌ๋œ ๊ณ„์ •์„ ์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž์— ๋“ฑ๋กํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

1. Windows API ํ•จ์ˆ˜ ์„ ์–ธ

(1) LogonUser

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool LogonUser(
    string lpszUsername,
    string lpszDomain,
    string lpszPassword,
    int dwLogonType,
    int dwLogonProvider,
    out IntPtr phToken
);
  • ์—ญํ• : ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•˜๊ณ , ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ๋ณด์•ˆ ํ† ํฐ(phToken)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜:
    • lpszUsername: ๋กœ๊ทธ์ธํ•  ์‚ฌ์šฉ์ž ์ด๋ฆ„.
    • lpszDomain: ์‚ฌ์šฉ์ž๊ฐ€ ์†ํ•œ ๋„๋ฉ”์ธ. ๋„๋ฉ”์ธ ์ด๋ฆ„์ด๋‚˜ ์ปดํ“จํ„ฐ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋กœ์ปฌ ์‚ฌ์šฉ์ž๋ผ๋ฉด .์œผ๋กœ ์ง€์ •.
    • lpszPassword: ์‚ฌ์šฉ์ž์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ.
    • dwLogonType: ๋กœ๊ทธ์ธ ์œ ํ˜•์„ ์ง€์ •ํ•˜๋Š” ์ƒ์ˆ˜. ์˜ˆ๋ฅผ ๋“ค์–ด, LOGON32_LOGON_NEW_CREDENTIALS๋Š” ์ƒˆ๋กœ์šด ์ž๊ฒฉ ์ฆ๋ช…์„ ์‚ฌ์šฉ.
    • dwLogonProvider: ๋กœ๊ทธ์ธ ๊ณต๊ธ‰์ž. ๊ธฐ๋ณธ์ ์œผ๋กœ LOGON32_PROVIDER_DEFAULT๋ฅผ ์‚ฌ์šฉ.
    • phToken: ํ˜ธ์ถœ์ด ์„ฑ๊ณตํ•˜๋ฉด ๋ฐ˜ํ™˜๋˜๋Š” ์‚ฌ์šฉ์ž ๋ณด์•ˆ ํ† ํฐ ํฌ์ธํ„ฐ.
  • ๋ฐ˜ํ™˜๊ฐ’:
    • true: ์ธ์ฆ ์„ฑ๊ณต.
    • false: ์ธ์ฆ ์‹คํŒจ. ์ž์„ธํ•œ ์˜ค๋ฅ˜ ์ฝ”๋“œ๋Š” Marshal.GetLastWin32Error()๋กœ ํ™•์ธ.

 

(2) CloseHandle 

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
  • ์—ญํ• : LogonUser๋กœ ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž ๋ณด์•ˆ ํ† ํฐ ํ•ธ๋“ค์„ ๋‹ซ์•„ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
  • ๋งค๊ฐœ๋ณ€์ˆ˜:
    • handle: ๋‹ซ์„ ํ•ธ๋“ค(LogonUser์—์„œ ๋ฐ˜ํ™˜๋œ phToken).
  • ๋ฐ˜ํ™˜๊ฐ’:
    • true: ์„ฑ๊ณต์ ์œผ๋กœ ํ•ธ๋“ค์ด ๋‹ซํž˜.
    • false: ํ•ธ๋“ค์„ ๋‹ซ๋Š” ๋ฐ ์‹คํŒจ. ์ถ”๊ฐ€ ์˜ค๋ฅ˜๋Š” Marshal.GetLastWin32Error()๋ฅผ ํ†ตํ•ด ํ™•์ธ.

 

2. Logon Constans

(1) LOGON32_LOGON_NEW_CREDENTIALS

private const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
  • ์˜๋ฏธ: ์ƒˆ๋กœ์šด ์ž๊ฒฉ ์ฆ๋ช…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„คํŠธ์›Œํฌ ๋ฆฌ์†Œ์Šค์— ์•ก์„ธ์Šคํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋กœ๊ทธ์ธ ์œ ํ˜•.
  • ์‚ฌ์šฉ ์‚ฌ๋ก€: ๋‹ค๋ฅธ ๊ณ„์ •์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ์ž„์‹œ๋กœ ๋นŒ๋ ค์™€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ.

 

(2) LOGON32_PROVIDER_DEFAULT

private const int LOGON32_PROVIDER_DEFAULT = 0;
  • ์˜๋ฏธ: ๊ธฐ๋ณธ ๋กœ๊ทธ์ธ ๊ณต๊ธ‰์ž๋ฅผ ์‚ฌ์šฉ.

 

3. ํ•„๋“œ ๋ฐ ์ปจํ…์ŠคํŠธ

(1) _userToken

private IntPtr _userToken = IntPtr.Zero;
  • ์—ญํ• : LogonUser๋กœ ๋ฐ˜ํ™˜๋œ ๋ณด์•ˆ ํ† ํฐ์„ ์ €์žฅ.
  • ์ดˆ๊ธฐ๊ฐ’: InPtr.Zero๋Š” ๋นˆ ํ† ํฐ์„ ๋‚˜ํƒ€๋ƒ„.

 

(2) _impersonationContext

private WindowsImpersonationContext _impersonationContext;
  • ์—ญํ• : WindowsIdentity.Impersonate๋กœ ์ƒ์„ฑ๋œ ์‚ฌ์šฉ์ž ์ปจํ…์ŠคํŠธ ์ €์žฅ
  • WindowsIdentity.Impersonate ์ด๋ž€: .NET์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ํŠน์ • Windows ์‚ฌ์šฉ์ž ๊ณ„์ •์„ ๊ฐ€์žฅ(Impersonate)ํ•˜์—ฌ ํ•ด๋‹น ๊ณ„์ •์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ ๋ชฉ์ : ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ์ž„์‹œ๋กœ ๊ฐ€์ ธ์™€ ์ž‘์—…์„ ์ˆ˜ํ–‰

 

4. ๋กœ๊ทธ์ธ ๋กœ์ง

Windows ์ž๊ฒฉ ์ฆ๋ช…(์‚ฌ์šฉ์ž ์ด๋ฆ„, ๋„๋ฉ”์ธ, ๋น„๋ฐ€๋ฒˆํ˜ธ)์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๊ณ„์ •์„ ์ธ์ฆํ•˜๊ณ , ํ•ด๋‹น ๊ณ„์ •์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์›๋ž˜์˜ ์‚ฌ์šฉ์ž ๊ณ„์ • ๋Œ€์‹  ๋‹ค๋ฅธ ๊ณ„์ •์˜ ๊ถŒํ•œ์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

(1) LogonUser ํ˜ธ์ถœ

bool success = LogonUser(username, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, out _userToken);
  • LogonUser ํ•จ์ˆ˜:
    • Windows API ํ•จ์ˆ˜๋กœ, ํŠน์ • ์ž๊ฒฉ ์ฆ๋ช…(username, domain, password)์œผ๋กœ ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆ.
  • ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…:
    • useername: ๋กœ๊ทธ์ธํ•˜๋ ค๋Š” ์‚ฌ์šฉ์ž ๊ณ„์ •์˜ ์ด๋ฆ„.
    • domain: ๊ณ„์ •์ด ์†ํ•œ ๋„๋ฉ”์ธ, ๋กœ์ปฌ, ์„œ๋ฒ„ ๊ณ„์ •์ด๋ผ๋ฉด "." ๋˜๋Š” ๋จธ์‹  ์ด๋ฆ„์„ ์‚ฌ์šฉ. ex)192.266.1.5
    • password: ํ•ด๋‹น ๊ณ„์ •์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ
    • LOGON32_LOGON_NEW_CREDENTIALS
      • ์ƒˆ๋กœ์šด ์ž๊ฒฉ ์ฆ๋ช…์œผ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰.
      • ๋„คํŠธ์›Œํฌ ๋ฆฌ์†Œ์Šค ์•ก์„ธ์Šค ๋“ฑ์—์„œ ์‚ฌ์šฉ.
    • LOGON32_PROVIDER_DEFAULT
      • ๊ธฐ๋ณธ ์ธ์ฆ ๊ณต๊ธ‰์ž๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์ •.
  • out _userToken:
    • ์„ฑ๊ณต ์‹œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ณด์•ˆ ํ† ํฐ์„ ์ €์žฅํ•  ๋ณ€์ˆ˜.
    • ์ดํ›„ WindowsIdentity๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ CloseHandle๋กœ ํ•ด์ œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ.

 

(2) ์—๋Ÿฌ ์ฒ˜๋ฆฌ

if (!success)
{
    int errorCode = Marshal.GetLastWin32Error();
    MessageBox.Show("์ธ์ฆ ์‹คํŒจ: ์˜ค๋ฅ˜ ์ฝ”๋“œ " + errorCode);
    return false;
}
  • LogonUser๊ฐ€ ์‹คํŒจํ•˜๋ฉด:
    • Marshal.GetLastWin32Error ๋ฅผ ํ†ตํ•ด Windows API์—์„œ ๋ฐœ์ƒํ•œ ๋งˆ์ง€๋ง‰ ์—๋Ÿฌ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ด.
    • ์‚ฌ์šฉ์ž์—๊ฒŒ ์‹คํŒจ ๋ฉ”์‹œ์ง€์™€ ์—๋Ÿฌ ์ฝ”๋“œ๋ฅผ ํ‘œ์‹œ.

 

(3) WindowsIdentity.Impersonate๋กœ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ ์ „ํ™˜

_impersonationContext = WindowsIdentity.Impersonate(_userToken);
  • WindowsIdentity.Impersonate:
    • _userToken์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ์Šค๋ ˆ๋“œ์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝ.
    • ์ดํ›„ ์ฝ”๋“œ๊ฐ€ ์ƒˆ๋กœ์šด ๊ณ„์ •(์ง€์ •๋œ ์‚ฌ์šฉ์ž ๊ณ„์ •)์˜ ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰.
  • ๋ฐ˜ํ™˜๊ฐ’:
    • WindowsImpersonationContext ๊ฐ์ฒด: ์ปจํ…์ŠคํŠธ๋ฅผ ๋ณต์›ํ•  ๋•Œ ์‚ฌ์šฉ.

 

(3) ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

catch (Exception ex)
{
    MessageBox.Show("์ธ์ฆ ์˜ค๋ฅ˜: " + ex.Message);
    return false;
}
  • ์ธ์ฆ ๋„์ค‘ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œ.
  • ์˜ˆ์™ธ์˜ ์›์ธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ex.Message๋ฅผ ์ถœ๋ ฅ.

 

5. ๋กœ๊ทธ์•„์›ƒ ๋กœ์ง

Windows ์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž์—์„œ ์ƒ์„ฑ๋œ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ , ์ด๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ๋œ ์ž์›์„ ์•ˆ์ „ํ•˜๊ฒŒ ํ•ด์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

์ธ์ฆ ์ž‘์—…์ด ๋๋‚œ ๋’ค ์ž์›์„ ์ •๋ฆฌํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฐ ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ๋ฐฉ์ง€ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

 

(1) ์ธ์ฆ ์ปจํ…์ŠคํŠธ ์ข…๋ฃŒ

if (_impersonationContext != null)
{
    _impersonationContext.Undo();
    _impersonationContext = null;
}
  • _impersonationContext:
    • ์ด์ „์— WindowsIdentity.Impersonate๋กœ ์ƒ์„ฑ๋œ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ๋‚˜ํƒ€๋ƒ„.
    • ํ˜„์žฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋‹ค๋ฅธ ๊ณ„์ •์˜ ๊ถŒํ•œ์œผ๋กœ ์ž‘์—…ใ…‡๋ฅด ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์Œ์„ ์˜๋ฏธ.
  • Undo():
    • ํ˜„์žฌ ์Šค๋ ˆ๋“œ์˜ ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ์›๋ž˜ ์ƒํƒœ(๋กœ๊ทธ์ธํ•œ ๊ณ„์ •์˜ ๊ถŒํ•œ)๋กœ ๋ณต์›.
    • ๋ณต์› ์ž‘์—… ํ›„_impersonationContext๋Š” ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ null๋กœ ์„ค์ •ํ•˜์—ฌ ์ •๋ฆฌ.
  • ํ•„์š”์„ฑ:
    • ๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์œผ๋ฉด ์ดํ›„ ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป๋œ ๊ณ„์ •์˜ ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Œ.

 

(2) ์‚ฌ์šฉ์ž ํ† ํฐ ํ•ด์ œ

if (_userToken != IntPtr.Zero)
{
    bool result = CloseHandle(_userToken);
    if (!result)
    {
        int errorCode = Marshal.GetLastWin32Error();
        MessageBox.Show("CloseHandle ์‹คํŒจ: ์˜ค๋ฅ˜ ์ฝ”๋“œ " + errorCode);
    }

    _userToken = IntPtr.Zero;
}
  • _userToken:
    • LogonUser ํ•จ์ˆ˜์—์„œ ๋ฐ˜ํ™˜๋œ ๋ณด์•ˆ ํ† ํฐ.
    • ์ž‘์—…์ด ๋๋‚œ ํ›„ ๋ฐ˜๋“œ์‹œ ํ•ด์ œํ•ด์•ผ ํ•จ.
  • CloseHandle(_userToken):
    • Windows API ํ•จ์ˆ˜๋กœ, ๋ณด์•ˆ ํ† ํฐ(_userToken)๊ณผ ๊ด€๋ จ๋œ ์‹œ์Šคํ…œ ์ž์›์„ ํ•ด์ œ.
    • ๋ฐ˜ํ™˜๊ฐ’์ด false์ธ ๊ฒฝ์šฐ ํ•ด์ œ ์‹คํŒจ๋ฅผ ์˜๋ฏธ.
  • Marshal.GetLastWin32Error():
    • CloseHandle ํ˜ธ์ถœ์ด ์‹คํŒจํ•œ ๊ฒฝ์šฐ, Windows API์—์„œ ๋ฐœ์ƒํ•œ ๋งˆ์ง€๋ง‰ ์—๋Ÿฌ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์˜ด.
    • ์—๋Ÿฌ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์—ฌ ์›์ธ์„ ํŒŒ์•…ํ•˜๊ณ , ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€๋กœ ํ‘œ์‹œ.
  • ํ•„์š”์„ฑ:
    • ๋ณด์•ˆ ํ† ํฐ์„ ํ•ด์ œํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ๋ณด์•ˆ ์ทจ์•ฝ์  ๋ฐœ์ƒ ๊ฐ€๋Šฅ

 

์ „์ฒด์ฝ”๋“œ

		// Windows API ํ•จ์ˆ˜ ์„ ์–ธ
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            out IntPtr phToken
        );

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private extern static bool CloseHandle(IntPtr handle);

        // Logon Constants
        private const int LOGON32_LOGON_NEW_CREDENTIALS = 9; // ์ƒˆ๋กœ์šด ์ž๊ฒฉ ์ฆ๋ช…์œผ๋กœ ๋กœ๊ทธ์ธ
        private const int LOGON32_PROVIDER_DEFAULT = 0;      // ๊ธฐ๋ณธ ์ธ์ฆ ์ œ๊ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์ •

        private IntPtr _userToken = IntPtr.Zero;
        private WindowsImpersonationContext _impersonationContext;
        
        // ์ž๊ฒฉ์ฆ๋ช…๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ
        private bool AuthenticateUser(string username, string domain, string password)
        {
            try
            {
                // ์ธ์ฆ์„ ์œ„ํ•œ Windows API ํ˜ธ์ถœ
                bool success = LogonUser(username, domain, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, out _userToken);
                if (!success)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    MessageBox.Show("์ธ์ฆ ์‹คํŒจ: ์˜ค๋ฅ˜ ์ฝ”๋“œ " + errorCode);
                    return false;
                }

                // ์ธ์ฆ ์ปจํ…์ŠคํŠธ ์‹œ์ž‘
                _impersonationContext = WindowsIdentity.Impersonate(_userToken);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("์ธ์ฆ ์˜ค๋ฅ˜: " + ex.Message);
                return false;
            }
         }
         
        // ์ž๊ฒฉ์ฆ๋ช…๊ด€๋ฆฌ์ž ๋กœ๊ทธ์•„์›ƒ
        private void LogoutUser()
        {
            try
            {
                // ์ธ์ฆ ์ปจํ…์ŠคํŠธ ์ข…๋ฃŒ ๋ฐ ์ž์› ํ•ด์ œ
                if (_impersonationContext != null)
                {
                    _impersonationContext.Undo();
                    _impersonationContext = null;
                }

                // ์‚ฌ์šฉ์ž ํ† ํฐ์„ ์•ˆ์ „ํ•˜๊ฒŒ ๋‹ซ๊ธฐ
                if (_userToken != IntPtr.Zero)
                {
                    bool result = CloseHandle(_userToken);
                    if (!result)
                    {
                        // CloseHandle ์‹คํŒจ ์‹œ ์—๋Ÿฌ ๋กœ๊ทธ ์ถ”๊ฐ€
                        int errorCode = Marshal.GetLastWin32Error();
                        // ์ ์ ˆํ•œ ๋กœ๊น… ๋˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
                        MessageBox.Show("CloseHandle ์‹คํŒจ: ์˜ค๋ฅ˜ ์ฝ”๋“œ " + errorCode);
                    }

                    _userToken = IntPtr.Zero;
                }
            }
            catch (Exception ex)
            {
                // ์˜ˆ์™ธ ์ฒ˜๋ฆฌ: ๋กœ๊ทธ์•„์›ƒ ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฅผ ์ฒ˜๋ฆฌ
                MessageBox.Show("๋กœ๊ทธ์•„์›ƒ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: " + ex.Message);
            }
         }
         
         
         // ์‹คํ–‰ ํ•จ์ˆ˜
         private bool action(){
         
         	// ์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ:username, domain, password
            if (!AuthenticateUser("username", "188.161.2.250", "password"))
            {
                MessageBox.Show("์ž๊ฒฉ ์ฆ๋ช… ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ์ด ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž‘์—…์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.");
                return false; // ๋กœ๊ทธ์ธ ์‹คํŒจ ์‹œ ์ž‘์—… ์ทจ์†Œ
            }
         	
            
            try
            {
            	
            	// ๊ณ„์ • ๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰ํ•  ์ž‘์—…
       			MessageBox.Show("์ธ์ฆ ์„ฑ๊ณต! ํ˜„์žฌ ์‚ฌ์šฉ์ž: " + WindowsIdentity.GetCurrent().Name);
            
            }
            finally
            {
                LogoutUser(); // ์ธ์ฆ ์ปจํ…์ŠคํŠธ ์ข…๋ฃŒ
            }
         
         }

 

 

 

728x90
๋ฐ˜์‘ํ˜•