| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This project aims to emulate the functionality provided by IIS Integration in an ASP.NET Core project that uses Windows Authentication for the purposes of testing with ASP.NET Core's TestServer from Microsoft.AspNetCore.TestHost.
It provides real, authenticated Windows Auth capabilities - not just a mock of such. The WindowsIdentity of the WindowsPrincipal that will be signed into your application can use all normal behaviors, including .Impersonate().
Visit Nuget.org to pull this library into your project as a dependency.
There are two main ways to use this library:
This project is designed to be used with xunit, but has no hard dependencies on it. The below example assumes xunit usage, but could easily be adapted for other test frameworks.
In your test project:
public class MyProjectServerFixture : WindowsAuthServerFixture<Startup>
{
// Override members as desired. Likely culprits are:
protected override string ApplicationName => "MyCompany.MyProject.Web";
// Path to the web project, relative to the working directory of the running test assembly.
// Default value (seen below) assumes test and web project live side-by-side.
// If projects are nested differently (e.g. separate 'src' and 'test' directories), modify as needed.
protected override string ContentRoot => $"../../../../{ApplicationName}";
}For more info about overridable members of WindowsAuthServerFixture, view this project's source code.
If the provided fixture doesn't fit your needs, or you already have your own fixture for a TestServer/WebHostBuilder, you can simply add the needed services yourself:
var builder = new WebHostBuilder()
// Your method calls go here...
.ConfigureServices(services =>
{
services.AddWindowsAuthenticationForTesting();
// ONLY IF AuthenticationMiddleware (UseAuthentication) isn't normally part of your pipeline:
services.AddTransient<IStartupFilter, AddAuthenticationStartupFilter>();
});You would then create a test fixture (or equivalent for your preferred test framework) that would expose a TestServer created from this WebHostBuilder.
Then, in an xunit test:
public class MyTests : IClassFixture<MyProjectServerFixture>
{
private readonly TestServer _server;
public MyTests(MyProjectServerFixture fixture)
{
_server = fixture.Server;
}
[Fact]
public async Task MyTest()
{
HttpClient client;
// Choose one:
client = _server.ClientForAnonymous();
client = _server.ClientForCurrentUser(); // Effectively: UseDefaultCredentials = true
client = _server.ClientForUser(new NetworkCredential("userName", "password", "DOMAIN"));
// Make requests against the HttpClient. The requests will be appropriately authenticated when they are handled by your web application, despite not running with IIS.
}
}| Back | FazBrowse Home | New Git URL |