2012-01-22 31 views
7

Tôi đang mạo danh một tài khoản người dùng thành công, nhưng tôi không thể sử dụng tài khoản mạo danh để liên kết với AD và kéo xuống DirectoryEntry.Mạo danh và DirectoryEntry

Mã đầu ra dưới đây:

  • Trước khi mạo danh tôi: DOMAIN \ user
  • Sau mạo danh tôi: DOMAIN \ admin
  • Lỗi: C: \ Users \ user \ ADSI_Impersonation \ bin \ debug \ ADSI_Impersonation.exe sAMAccountName:

vấn đề của tôi dường như tương tự như:

How to use the System.DirectoryServices namespace in ASP.NET

Tôi nhận được mã thông báo chính. Tôi hiểu rằng tôi cần sử dụng ủy quyền để sử dụng mã thông báo mạo danh trên máy tính từ xa. Tôi xác nhận rằng tài khoản không có cờ được chọn "Tài khoản nhạy cảm và không thể được ủy quyền". Tôi cũng khẳng định rằng chính sách Local Group Policy và Domain Group không ngăn cản phái đoàn:

Computer Configuration \ Windows Settings \ Security Settings \ Local Phân Policies \ Quyền User \

tôi đang thiếu gì?

Cảm ơn!

using System; 
using System.DirectoryServices; 
using System.Security; 
using System.Security.Principal; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 
using System.Runtime.ConstrainedExecution; 

namespace ADSI_Impersonation 
{ 
    class Program 
    { 
     [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
     public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
      int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken); 

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

     static void Main(string[] args) 
     { 
      const int LOGON32_PROVIDER_DEFAULT = 0; 
      const int LOGON32_LOGON_INTERACTIVE = 2; 

      string userName = "[email protected]"; 
      string password = "password"; 

      Console.WriteLine("Before impersonation I am: " + WindowsIdentity.GetCurrent().Name); 

      SafeTokenHandle safeTokenHandle; 

      try 
      { 
       bool returnValue = LogonUser(userName, null, password, 
        LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, 
        out safeTokenHandle); 

       if (returnValue) 
       { 
        WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()); 
        WindowsImpersonationContext impersonatedUser = newId.Impersonate(); 
       } 
       else 
       { 
        Console.WriteLine("Unable to create impersonatedUser."); 
        return; 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Authentication error.\r\n" + e.Message); 
      } 

      Console.WriteLine("After impersonation I am: " + WindowsIdentity.GetCurrent().Name); 

      string OU = "LDAP://dc=domain,dc=com"; 
      DirectoryEntry entry = new DirectoryEntry(OU); 
      entry.AuthenticationType = AuthenticationTypes.Secure; 

      DirectorySearcher mySearcher = new DirectorySearcher(); 
      mySearcher.SearchRoot = entry; 
      mySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree; 
      mySearcher.PropertiesToLoad.Add("cn"); 
      mySearcher.PropertiesToLoad.Add("samaccountname"); 

      string cn = "fistname mi. lastname"; 
      string samaccountname = ""; 

      try 
      { 
       // Create the LDAP query and send the request 
       mySearcher.Filter = "(cn=" + cn + ")"; 

       SearchResultCollection searchresultcollection = mySearcher.FindAll(); 

       DirectoryEntry ADentry = searchresultcollection[0].GetDirectoryEntry(); 

       Console.WriteLine("samaccountname: " + ADentry.Properties["samaccountname"].Value.ToString()); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Error: " + e.Message); 
      } 

      Console.WriteLine("samaccountname: " + samaccountname); 
      Console.ReadLine(); 
     } 
    } 

    public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid 
    { 
     private SafeTokenHandle() 
      : base(true) 
     { 
     } 

     [DllImport("kernel32.dll")] 
     [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] 
     [SuppressUnmanagedCodeSecurity] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     private static extern bool CloseHandle(IntPtr handle); 

     protected override bool ReleaseHandle() 
     { 
      return CloseHandle(handle); 
     } 
    } 
} 

Trả lời

1

Nhiều API .NET không tính đến việc mạo danh thủ công của bạn, chẳng hạn như các truy vấn LDAP mà bạn nhận thấy. Vì vậy, bạn cần phải sử dụng các nhà thầu quá tải của DirectoryEntry thay vào đó,

http://msdn.microsoft.com/en-us/library/bw8k1as4.aspx

http://msdn.microsoft.com/en-us/library/wh2h7eed.aspx

+0

Cảm ơn bạn đã xác nhận rằng đó là cách duy nhất để thực hiện điều đó. Tôi đoán vì tôi sẽ có tên người dùng/mật khẩu của người dùng không có lý do gì sẽ không hoạt động. –

+0

@Lex Li Tôi hỏi một câu hỏi liên quan bạn vui lòng kiểm tra xem nó: http://stackoverflow.com/questions/18842970/asp-net-imperonate-in-netframework-2-vs-netframework-4 –

0

Lỗi (0x80004005): Lỗi Unspecified

tôi đã một số vấn đề kết nối với các cửa sổ từ xa thực với lỗi Lỗi (0x80004005): Lỗi không xác định. Tôi đã giải quyết như sau:

//Define path 
//This path uses the full path of user authentication 
String path = string.Format("WinNT://{0}/{1},user", server_address, username); 
DirectoryEntry deBase = null; 
try 
{ 
    //Try to connect with secure connection 
    deBase = new DirectoryEntry(path, username, _passwd, AuthenticationTypes.Secure); 

    //Connection test 
    //After test define the deBase with the parent of user (root container) 
    object nativeObject = deBase.NativeObject; 
    deBase = deBase.Parent; 

} 
catch (Exception ex) 
{ 
    //If an error occurred try without Secure Connection 
    try 
    { 
     deBase = new DirectoryEntry(path, username, _passwd); 

     //Connection test 
     //After test define the deBase with the parent of user (root container) 
     object nativeObject = deBase.NativeObject; 
     deBase = deBase.Parent; 
     nativeObject = deBase.NativeObject; 

    } 
    catch (Exception ex2) 
    { 
     //If an error occurred throw the error 
     throw ex2; 
    } 
} 

Hy vọng điều đó sẽ hữu ích. Helvio Junior www.helviojunior.com.br

Các vấn đề liên quan