ฉันมีเว็บไซต์ ASP.NET MVC ที่ใช้ Windows Authentication เพื่อควบคุมการเข้าถึง ฉันต้องการให้มีการทดสอบซีลีเนียม specflow ที่ตรวจสอบว่าการกำหนดค่าถูกต้องโดยพยายามเยี่ยมชมไซต์ในฐานะผู้ใช้ที่ไม่ได้รับอนุญาต
เนื่องจากเราใช้บัญชีโดเมนเพื่อควบคุมการเข้าถึง จึงไม่มีหน้าจอเข้าสู่ระบบชื่อผู้ใช้/รหัสผ่าน ข้อมูลรับรองของผู้ใช้ปัจจุบันจะถูกส่งไปยังไซต์โดยอัตโนมัติโดยเบราว์เซอร์
ดังนั้นสำหรับการทดสอบซีลีเนียมของฉัน ฉันจำเป็นต้องเรียกใช้ 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(..)
- person ddavison   schedule 20.01.2015runas /user:USER@DOMIAN path\to\mstest.exe ...
- person Greg Burghardt   schedule 28.01.2015