본문 바로가기
IT-개발,DB

[개발/C#] 네트웍 공유 폴더 사용시 자격증명 방법

by SB리치퍼슨 2010. 7. 2.

파워빌더 호출 사용 예

Function Integer LogonUserA(String lpszUsername,String lpszDomain,String lpszPassword,integer dwLogonType,Integer dwLogonProvider,REF long phToken) Library "advapi32.dll"
또는 이렇게 선언해서 사용해도 된다.

Function boolean LogonUser(String lpszUsername,String lpszDomain,String lpszPassword, ulong dwLogonType,ulong dwLogonProvider,REF ulong phToken) Library "advapi32.dll" alias for "LogonUserA"


ulong lul_token
boolean lb_ret

// command button clicked event
lb_ret LogonUser(username,domainname,LOGON32_LOGON_NETWORK,LOGON32_PROVIDER_DEFAULT, lul_token)

if lb_ret  then
  messagebox("","logon success")
else
  messagebox("","login failed")
end if

================================================

C#

네트웍 공유 폴더 를 사용 할때 사용자별 자격증명을 하기 위하여 사용된다.
(이는 System.IO Control을 사용할 때 유용하다)

=== Class 생성 ===

 using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Security.Principal;

namespace M.Lib.WndLogin {
    /// <summary>
    /// 로그온 타입
    /// </summary>
    public enum LogonType {
        LOGON32_LOGON_INTERACTIVE = 2,
        LOGON32_LOGON_NETWORK = 3,
        LOGON32_LOGON_BATCH = 4,
        LOGON32_LOGON_SERVICE = 5,
        LOGON32_LOGON_UNLOCK = 7,
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
        LOGON32_LOGON_NEW_CREDENTIALS = 9
    }

    /// <summary>
    /// 로그온 제공자
    /// </summary>
    public enum LogonProvider {
        LOGON32_PROVIDER_DEFAULT = 0,
        LOGON32_PROVIDER_WINNT35 = 1,
        LOGON32_PROVIDER_WINNT40 = 2,
        LOGON32_PROVIDER_WINNT50 = 3
    }

    public class WndLogin {
        /// <summary>
        /// 로그온
        /// </summary>
        /// <param name="lpszUsername">사용자 계정</param>
        /// <param name="lpszDomain">도메인</param>
        /// <param name="lpszPassword">암호</param>
        /// <param name="dwLogonType">로그온 조류</param>
        /// <param name="dwLogonProvider">로그온 프로바이더</param>
        /// <param name="phToken">엑세스 토큰</param>
        /// <returns></returns>
        [DllImport!("advapi32.dll", EntryPoint = "LogonUser", SetLastError = true)]
        private static extern bool _LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
            int dwLogonType, int dwLogonProvider, out int phToken);

        /// <summary>
        /// 주어진 사용자 ID로 로그온하고 액세스 토큰을 반환
        /// </summary>
        /// <param name="userName">사용자 계정</param>
        /// <param name="password">암호</param>
        /// <param name="domainName">도메인 이름</param>
        /// <param name="logonType">로그온 종류</param>
        /// <param name="logonProvider">로그온 프로바이더</param>
        /// <returns>엑세스 토큰</returns>
        public static IntPtr LogonUser(string userName, string password, string domainName,
            LogonType logonType, LogonProvider logonProvider) {
            int token = 0;
            bool logonSuccess = _LogonUser(userName, domainName, password,
                (int)logonType, (int)logonProvider, out token);

            if (logonSuccess)
                return new IntPtr(token);

            int retval = Marshal.GetLastWin32Error();
            throw new Win32Exception(retval);
        }
    }
}

  === 사용방법 ===

using M.Lib.WndLogin;

 IntPtr token = WndLogin.LogonUser("계정", "암호", ".",
            LogonType.LOGON32_LOGON_NETWORK_CLEARTEXT, LogonProvider.LOGON32_PROVIDER_DEFAULT);
        WindowsIdentity identity = new WindowsIdentity(token);
        WindowsImpersonationContext ctx = identity.Impersonate();


================
다른 적용 예

const int ERROR_INSUFFICIENT_BUFFER = 122;

  [DllImport("advapi32.dll", EntryPoint = "LogonUser", SetLastError = true)]
  private static extern bool _LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);

  /// <summary>
  /// 주어진 사용자 ID로 로그온하여 액세스 토큰을 반환한다.
  /// </summary>
  /// <param name="userName">사용자 ID</param>
  /// <param name="password">암호</param>
  /// <param name="domainName">도메인 이름</param>
  /// <param name="logonType">로그온 종류</param>
  /// <param name="logonProvider">로그온 프로바이더</param>
  /// <returns></returns>
  public static WindowsImpersonationContext LogonUser(string userName, string password, string domainName, LogonType logonType, LogonProvider logonProvider)
  {
   int token = 0;
   bool logonSuccess = _LogonUser(userName, domainName, password, (int)logonType, (int)logonProvider, out token);
   if (logonSuccess)
    return WindowsIdentity.Impersonate(new IntPtr(token));

   int retval = Marshal.GetLastWin32Error();
   throw new Win32Exception(retval);
  }

  /// <summary>
  /// 로그인사용자 로그오프
  /// </summary>
  /// <param name="ctx"></param>
  public static void LogoutUser(WindowsImpersonationContext ctx)
  {
   ctx.Undo();
  }

 


 /// <summary>
 /// LogonUser API에서 사용하는 로그온 타입
 /// </summary>
 public enum LogonType
 {
  LOGON32_LOGON_INTERACTIVE = 2,
  LOGON32_LOGON_NETWORK = 3,
  LOGON32_LOGON_BATCH = 4,
  LOGON32_LOGON_SERVICE = 5,
  LOGON32_LOGON_UNLOCK = 7,
  LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
  LOGON32_LOGON_NEW_CREDENTIALS = 9
 }

 public enum LogonProvider
 {
  LOGON32_PROVIDER_DEFAULT = 0,
  LOGON32_PROVIDER_WINNT35 = 1,
  LOGON32_PROVIDER_WINNT40 = 2,
  LOGON32_PROVIDER_WINNT50 = 3
 }

 

 

 


WindowsImpersonationContext ctx = null;

  if (!logonUserID.Equals("")) ctx = COasisPC.LogonUser(logonUserID, logonPasswd, ".", LogonType.LOGON32_LOGON_NETWORK_CLEARTEXT, LogonProvider.LOGON32_PROVIDER_DEFAULT);

   .. 일반코드

   // 사용자 인증에서 인터넷사용자 인증으로 변경
   if (ctx != null) COasisPC.LogoutUser(ctx);

반응형

댓글