【发布时间】:2015-03-18 17:05:45
【问题描述】:
我有一个使用 Windows 身份验证来控制访问的 ASP.NET MVC 网站。我想要一个 specflow selenium 测试,通过尝试以非授权用户身份访问该站点来检查配置是否正确。
由于我们使用域帐户来控制访问,因此没有用户名/密码登录屏幕。当前用户的凭据由浏览器自动传递给站点。
所以对于我的 Selenium 测试,我需要能够以特定用户身份运行 Internet Explorer。
我找到了许多关于 windows 模拟的文章,我可以在测试运行期间切换到我的测试用户(使用来自 http://support.microsoft.com/kb/306158 的代码)。但是,如果我随后创建 InternetExplorerDriver,它会使用我的凭据而不是测试用户的凭据启动 Internet Explorer(尽管这个问题和答案表明它应该可以工作 https://sqa.stackexchange.com/questions/2277/using-selenium-webdriver-with-windows-authentication)。
我也可以作为我的测试用户显式启动 Internet Explorer 进程,但我看不到将 InternetExplorerDriver 绑定到已经运行的 Internet Explorer 进程的方法,因此这可能是死路一条。
下面是我的代码,基本上取自上面的 MSDN 页面。在调试器中,我可以看到 WindowsIdentity.GetCurrent().Name 在测试的所有步骤中都是“testUser”。
namespace MyProject.Specs
{
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using TechTalk.SpecFlow;
[Binding]
public class AuthorisationSteps
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
private static WindowsImpersonationContext impersonationContext;
private static IWebDriver driver;
[BeforeScenario]
public static void impersonateUser()
{
if (!impersonateValidUser("testUser", "testDomain", "password"))
{
throw new Exception();
}
driver = new InternetExplorerDriver();
}
[AfterScenario]
public static void cleanupUser()
{
undoImpersonation();
driver.Quit();
}
[Given(@"I am an unauthorised user")]
public void GivenIAmAnUnauthorisedUser()
{
var temp = WindowsIdentity.GetCurrent().Name;
}
[When(@"I go to the home page")]
public void WhenIGoToTheHomePage()
{
var temp = WindowsIdentity.GetCurrent().Name;
driver.Navigate().GoToUrl(BaseUrl);
}
[Then(@"I should see an error page")]
public void ThenIShouldSeeAnErrorPage()
{
var temp = WindowsIdentity.GetCurrent().Name;
Assert.That(driver.Title.Contains("Error"));
}
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
private static bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
var token = IntPtr.Zero;
var tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
return false;
}
private static void undoImpersonation()
{
impersonationContext.Undo();
}
}
}
【问题讨论】:
-
也许可以尝试一下:将
driver实例化放在impersonateValidUser(..)之前 -
感谢您的想法,但恐怕它不能解决问题
-
也许可以尝试通过
runas /user:USER@DOMIAN path\to\mstest.exe ...从Windows命令行开始你的测试 -
这可能行得通,但您需要为每个用户踢一次。现在我已经停止研究这个,因为我只有两个需要它的测试,我可以在几分钟内手动运行它们。
-
有人在 Java 的上下文中提出了类似的问题:stackoverflow.com/questions/18017883/… - 但它仍然“未回答”。
标签: asp.net-mvc selenium selenium-webdriver windows-authentication specflow