Bra*_*dis 5 authentication watin
我正在尝试为使用集成身份验证的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();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它会打印我试图模拟到控制台的用户名,但是网页显示我当前登录的用户,而不是我应该模仿的用户.
类似的问题发现于:
使用AzMan实现的授权方案的自动测试
模拟是很棘手的,我从来没有能够让 IE 使用 WatiN 作为另一个用户上下文运行。过去,我部署了另一个版本的站点,并在启用基本身份验证的情况下进行测试,然后通过对话框登录。
请查看以下博客以获取更多信息和示例代码:
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。启动 IE 后,您可以使用此处的代码通过模拟来附加 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 Files\\Internet Explorer\\iexplore.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, 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();
// domain\username
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);
}
Run Code Online (Sandbox Code Playgroud)
此代码需要重构,并且无法在带有 IE7 的 Vista 上运行,因为 IE8 中修复了 IE 错误。
| 归档时间: |
|
| 查看次数: |
2845 次 |
| 最近记录: |