Watin Windows身份验证
我正在尝试为使用集成身份验证的Intranet应用程序编写Watin测试.我正在尝试测试的网页打印Page.User.Identity.Name.
以下是我测试的一些代码: if (Win32.LogonUser(u.UserName,u.Domain,u.Password,2 /*LOGON32_LOGON_INTERACTIVE*/,0 /*LOGON32_PROVIDER_DEFAULT*/,out hToken)) { if (Win32.DuplicateToken(hToken,2,out hTokenDuplicate)) { WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate); WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate(); Console.WriteLine(WindowsIdentity.GetCurrent().Name); using (IE ie = new IE(url)) { Console.WriteLine(ie.ContainsText(u.UserName)); ie.AutoClose = false; } impersonationContext.Undo(); } } 当我运行它时,它会打印我试图模拟到控制台的用户名,但是网页显示我当前登录的用户,而不是我应该模仿的用户. 类似问题发现于:
模拟是棘手的,我从来没有能够使用WatiN作为另一个用户上下文运行IE.在过去,我已经部署了另一个正在测试的网站版本,并启用了基本身份验证,然后通过对话框登录.
有关更多信息和示例代码,请查看以下博客: http://blogs.msdn.com/jimmytr/archive/2007/04/14/writing-test-code-with-impersonation.aspx http://blogs.msdn.com/shawnfa/archive/2005/03/21/400088.aspx 编辑:我今天工作了.诀窍是你需要分开IE的启动和IE的自动化,因为你不能只在一次点击中完成它们. 首次启动即使用System.Diagnostics.Process.启动IE后,您可以使用here中的代码使用impersionation附加和与IE通信 这是代码 [TestMethod] public void TestMethod() { SecureString password = new SecureString(); password.AppendChar('p'); password.AppendChar('a'); password.AppendChar('s'); password.AppendChar('s'); password.AppendChar('w'); password.AppendChar('o'); password.AppendChar('r'); password.AppendChar('d'); ProcessStartInfo psi = new ProcessStartInfo(); psi.UserName = "localtest"; psi.Password = password; psi.UseShellExecute = false; psi.LoadUserProfile = true; psi.FileName = "c:Program FilesInternet Exploreriexplore.exe"; psi.Arguments = "about:blank"; Process proc = new Process(); proc.StartInfo = psi; proc.Start(); t.Join(); proc.Kill(); } private static void DoWorkAs(object o) { User u = o as User; IntPtr hToken = IntPtr.Zero; IntPtr hTokenDuplicate = IntPtr.Zero; if (Win32.LogonUser(u.UserName,out hToken)) { if (Win32.DuplicateToken(hToken,out hTokenDuplicate)) { WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate); WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate(); // domainusername Console.WriteLine(" Thread 2 : " + WindowsIdentity.GetCurrent().Name); IE ie = IE.AttachToIE(Find.ByUrl("about:blank")); ie.GoTo(@"http://www.google.com/"); ie.TextField(Find.ByName("q")).TypeText("WatiN"); ie.Button(Find.ByName("btnG")).Click(); Assert.IsTrue(ie.ContainsText("WatiN")); ie.GoTo("about:blank"); //revert impersonationContext.Undo(); Console.WriteLine(WindowsIdentity.GetCurrent().Name); } } if (hToken != IntPtr.Zero) Win32.CloseHandle(hToken); if (hTokenDuplicate != IntPtr.Zero) Win32.CloseHandle(hTokenDuplicate); } public class User { public User(string u,string d,string p) { Domain = d; UserName = u; Password = p; } public string UserName; public string Domain; public string Password; } public class Win32 { // P/Invoke snask [DllImport("advapi32.dll",SetLastError = true)] public static extern bool LogonUser( string lpszUsername,string lpszDomain,string lpszPassword,int dwLogonType,int dwLogonProvider,out IntPtr phToken); [DllImport("advapi32.dll",SetLastError = true)] public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,int SECURITY_IMPERSONATION_LEVEL,out IntPtr DuplicateTokenHandle); [DllImport("kernel32.dll",SetLastError = true)] public static extern bool CloseHandle(IntPtr hHandle); } 这段代码需要一个重构,并且在IE7上使用Vista工作,因为在IE8中修复了IE错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- WIN2003?IIS默认不支持ASP的问题
- windows-server-2003 – 在Windows更新后阻止服务器重新启动
- windows phone 7 – 本地化Windows Phone 7应用程序
- windows – 由于任何原因在断开连接时自动重新连接VPN – 持
- 如何通过Windows shell(bat,cmd)获取系统上的驱动器号列表?
- 安装Windows10出现无法识别磁盘时的解决方案
- file-io – 无法从WinRT中的xunit访问文件(该进程没有包标识
- windows-server-2003 – 我的繁忙文件服务器上的MFT碎片可能
- 从命令行运行Elevated Powershell提示符
- Microsoft Teams Voice语音落地系列-4 实战:Teams语音路由规
- windows-server-2003 – 重新启动后Windows 2003
- Exp2 后门原理与实践 - 20164304 姜奥
- 视频 – Windows Media Foundation使用IMFTransf
- windows-server-2012-r2 – 将Google Chrome配置
- Windows下安装MySQL
- windows-server-2003 – 诊断组策略对象无法访问
- Windows 2012 R2 中的AD DS部署
- windows – 如何确定计算机使用的网络适配器?
- Vagrant无法识别x64-Windows7上的VirtualBox(已经
- 如果我在代码中进行了更改,是否需要重新安装Wind