Tôi đang cố gắng thử nghiệm ứng dụng web ASP.NET MVC4 kết nối với những người dùng khác nhau cùng một lúc bằng cách sử dụng mạo danh. Vì vậy, ý tưởng cho thử nghiệm là để thực hiện một số trường hợp của trình điều khiển với một người dùng mạo danh khác nhau. Nghe có vẻ dễ hiểu ...IEDriverServer gây ra lỗi khi sử dụng mạo danh
Tuy nhiên, nếu sau khi gọi LogonUser với một người dùng khác từ hiện tại, bạn chạy IEDriverServer, hộp thoại lỗi ứng dụng sẽ hiển thị khi nó bắt đầu. Đây là thông tin được đăng ký trong EventLog:
Faulting application name: IEDriverServer.exe, version: 2.44.0.0, time stamp: 0x54496690
Faulting module name: IED7543.tmp, version: 0.0.0.0, time stamp: 0x5449668c
Exception code: 0xc0000005
Fault offset: 0x000000000009d609
Faulting process id: 0x760
Faulting application start time: 0x01d00e3d12f93475
Faulting application path: ...\bin\IEDriverServer.exe
Faulting module path: C:\Users\user\AppData\Local\Temp\IED7543.tmp
Report Id: 5383a54d-7a30-11e4-b39c-000c29b46927
Đây là mã tôi đang sử dụng để mạo danh. Nó có trụ sở tại tất cả các ví dụ tôi đã tìm thấy, vì vậy tôi không có bất ngờ ... Chúng tôi cũng đã cố gắng để sử dụng packege SimpleImpersonation với kết quả tương tự:
public class ImpersonationHelper
{
public enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
Cleartext = 8, // Win2K or higher
NewCredentials = 9 // Win2K or higher
};
public enum LogonProvider
{
Default = 0,
Winnt35 = 1,
Winnt40 = 2,
Winnt50 = 3
};
public enum ImpersonationLevel
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel, ref IntPtr hNewToken);
public sealed class Impersonator : IDisposable
{
public string Domain { get; private set; }
public string User { get; private set; }
public string Password { get; private set; }
public LogonType Type { get; private set; }
public LogonProvider Provider { get; private set; }
private WindowsImpersonationContext _context;
private IntPtr _token;
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public Impersonator(string domain, string user, string password,
LogonType type = LogonType.Interactive,
LogonProvider provider = LogonProvider.Default)
{
Domain = domain;
User = user;
Password = password;
Type = type;
Provider = provider;
_token = IntPtr.Zero;
Logon();
}
public void Dispose()
{
Undo();
}
private void Logon()
{
try
{
if (!LogonUser(User, Domain, Password, (int)Type, (int)Provider, out _token))
{
int ret = Marshal.GetLastWin32Error();
throw new Exception(String.Format("LogonUser failed with error code : {0}", ret));
}
_context = WindowsIdentity.Impersonate(_token);
}
catch (Exception exception)
{
var message = exception.Message;
Undo();
}
}
private void Undo()
{
try
{
if (_token != IntPtr.Zero)
{
CloseHandle(_token);
_token = IntPtr.Zero; // Clean _token so Undo() could be called several times
}
if (_context != null)
{
_context.Undo();
_context = null; // Clean _context so Undo() could be called several times
}
}
catch (Exception exception)
{
var message = exception.Message;
// Releasing resources failed...
}
}
}
}
Mã làm tăng ngoại lệ là:
using (new ImpersonationHelper.Impersonator("WIN-NKLTTMMUEPD", "tester", "tester"))
{
using (IWebDriver driver = new InternetExplorerDriver())
{
driver.Url = _appAddress;
return null;
}
}
Có ai biết làm cách nào để ngăn lỗi này xuất hiện không? Cảm ơn bạn rất nhiều trước.
Câu trả lời của bạn có đầy đủ các thông tin có giá trị để tiếp tục nghiên cứu cách thực hiện các thử nghiệm liên quan đến nhiều người dùng. Cảm ơn nỗ lực của bạn! Lỗi, tuy nhiên, hóa ra là một vấn đề liên quan đến phiên bản x64 của trình điều khiển. – yeyeyerman
Xin lỗi vì tôi không thể giải quyết vấn đề chính xác của bạn. – Dennis