2012-11-29 2 views
0

서버에서 일부 SQL 작업을 수행하기 위해 특정 사용자를 가장하려고합니다. 이것은 ASP.Net 응용 프로그램이 아닙니다. 전에 제공된 코드를 사용했는데 작동했습니다. 그러나 최근에는 Windows Server 2000에서 Windows Server 2008 R2로 환경을 업그레이드했습니다. 이 업그레이드가 끝나면이 코드가 저를 위해 작동하지 않습니다. 이 문제를 이해하고 해결하는 데 도움이 필요합니다. 모든 도움을 주시면 감사하겠습니다. 감사.Windows Server 2008의 가장

제공된 코드는 의사 코드이며 파일에 쓰려고하고 SQL 작업을 수행합니다.

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.IO; 
using System.Security.Principal; 
using System.Security.Permissions; 

[assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode = true)] 
[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")] 
public class Test 
{ 
    const int LOGON32_LOGON_INTERACTIVE = 2; 
    const int LOGON32_LOGON_NETWORK = 3; 
    const int LOGON32_LOGON_BATCH = 4; 
    const int LOGON32_LOGON_SERVICE = 5; 
    const int LOGON32_LOGON_UNLOCK = 7; 
    const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
    const int LOGON32_LOGON_NEW_CREDENTIALS = 9; 
    const int LOGON32_PROVIDER_DEFAULT = 0; 
    const int SecurityImpersonation = 2; 

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

    [DllImport("advapi32.dll", SetLastError = true)] 
    public static extern int ImpersonateLoggedOnUser(
     IntPtr hToken 
    ); 

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, 
     int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle); 

    [DllImport("advapi32.dll", SetLastError = true)] 
    static extern int RevertToSelf(); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    static extern int CloseHandle(IntPtr hObject); 

    public void TestImpersonation() 
    {    
     IntPtr lnToken = new IntPtr(0); 
     IntPtr dupeTokenHandle = new IntPtr(0); 
     StringBuilder sb = new StringBuilder(); 

     int TResult = LogonUser("itservices", "DFC", "St4hls345t", LOGON32_LOGON_NETWORK, 
       LOGON32_PROVIDER_DEFAULT, out lnToken); 
     if (TResult > 0) 
     { 
      bool retVal = DuplicateToken(lnToken, SecurityImpersonation, ref dupeTokenHandle); 
      if (false == retVal) 
      { 
       CloseHandle(lnToken); 
       Console.WriteLine("Exception thrown in trying to duplicate token."); 
       return; 
      } 

      WindowsIdentity newId = new WindowsIdentity(dupeTokenHandle); 
      WindowsImpersonationContext impersonatedUser = newId.Impersonate(); 

      writeLog(DateTime.Now.ToString(@"MM-dd-yyyy HH:mm:ss") + " - Impersonation Applied" + Environment.NewLine); 
      runQuery(); 
      impersonatedUser.Undo(); 
      writeLog(DateTime.Now.ToString(@"MM-dd-yyyy HH:mm:ss") + " - Impersonation Reverted" + Environment.NewLine); 
      runQuery(); 
      CloseHandle(lnToken); 
     } 
     else 
     { 
      writeLog(DateTime.Now.ToString(@"MM-dd-yyyy HH:mm:ss") + " - Impersonation not Applied" + Environment.NewLine); 
     } 

     return; 
    } 

    void writeLog(string message) 
    { 
     try 
     { 
      string filePath = @"E:\Impersonate\Testlog.txt"; 
      File.AppendAllText(filePath, message); 
     } 
     catch 
     { 
      Console.WriteLine(); 
     } 
    } 

    void runQuery() 
    { 
     SQLOperations sqlUtill = new SQLOperations(); 
     string cmdTxt = "SELECT * FROM [tblChildOrder] where [StahlsWorkOrderID] = 'DREAMFUL0015799'"; 
     DataTable dt = sqlUtill.executeQuery(cmdTxt); 
     if (dt != null) 
     { 
      Console.WriteLine(); 
     } 
     else 
     { 
      Console.WriteLine(); 
     } 
    } 
} 
+0

누구나 아이디어가 있으십니까? – Teja

답변

0

대개 내 코드를 위반 한 업그레이드는 대개 사용자에 대한 권한 변경 권한 변경으로 인해 발생했습니다. 사용자와 사용자의 권한을 다시 확인하면 문제를 찾아야합니다.