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);
}
}
}
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. –
@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 –