Simulating IIS App Pool Identity in ASP.Net Core Integration Test

Marc Chu

I'm attempting to write integration tests for an ASP.Net Core 3.1 API that I'm writing. I'm using the WebApplicationFactory<> class in order to create an HttpClient for calling into the API.

_Factory = new WebApplicationFactory<Startup>()
            .WithWebHostBuilder(b =>
            {
                b.ConfigureTestServices(services =>
                {
                    services.AddAuthentication("Test")
                        .AddScheme<AuthenticationSchemeOptions, MyAuthHandler>("Test", options => { });
                    RegisterTestServices(services);
                });
            });

With:

public class MyAuthHandler: AuthenticationHandler<AuthenticationSchemeOptions>
{
    public MyAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) { }
    protected override  Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        var claims = new[] {new Claim("preferred_username", "mchu")};
        var identity = new ClaimsIdentity(claims, "Test");
        var principal = new ClaimsPrincipal(identity);
        var ticket = new AuthenticationTicket(principal, "Test");

        var result = AuthenticateResult.Success(ticket);

        return Task.FromResult(result);
    }
}

This successfully allows me to retrieve an HttpClient that can call out to my endpoints (decorated with [Authorize]) so I don't get 401 errors. However, some of my tests are failing due to permissions problems upon attempting to access network shares.

In an actual IIS deployment, our application pool would be using an identity that has read/write access to those shares.

Is there anything I can do to ensure that my API code runs with permissions that can access those protected resources? Our existing integration test code (for non-ASP.Net Core code--this is a conversion) gets around this problem by simply performing the particular operations in an administrator impersonation context. I'm fine with doing that for these new tests, as well.

However, without yet understanding a whole lot about WebApplicationFactory<> (or any bit of the ASP.Net Core middleware pipeline, really), I'm not sure how I would go about doing this.

Elendil Zheng-MSFT

ASP.NET Core doesn't implement impersonation. Apps run with the app's identity for all requests, using app pool or process identity. If the app should perform an action on behalf of a user, use WindowsIdentity.RunImpersonated in a terminal inline middleware in Startup.Configure. Run a single action in this context and then close the context.

More details you can refer to this document

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP .Net Core 2 Self Contained Deployment giving 502.5 Error in IIS under different App Pool Identity Account

SAML integration with ASP.NET Core Identity

How to write Integration Test for Identity Server 4 in asp.net mvc core

API Integration Test for Login controller using Identity server4 in asp.net core

CPU, memory usage, thread pool usage - ASP NET core Identity not confirmed mail users delete handling - In app or separate app?

ASP.NET Core 3.1 run on IIS .NET CLR v4.0 and integrated managed must be a pool for each APP?

Asp.net Core Identity unit test controller actions

Append test project appSettings to ASP.NET Core integration tests

AppSettings.json for Integration Test in ASP.NET Core

Resolving dependencies in Integration test in ASP.NET Core

Use HttpTest inside asp.net core/grpc integration test

Integration tests with asp.net core (test of controllers without the views)

Integration test Asp.Net Core attribute parameter modification

Extension methods for Identity Controllers in ASP.Net Core 2.2 App

Add a new table into the ASP .NET Core MVC app with Identity

Seed test data for every test in ASP.NET Core / EF Core / xUnit.NET integration tests

Cannot launch asp.net core web app in IIS Express

How to host a asp.net core MVC app in IIS?

ASP.NET Core app targeting full framework — InProcess IIS?

Deploying ASP.NET Core 2.1 React App on IIS server

Publishing an Angular 6 app with Asp.Net Core 2.1.1 on IIS

Asp.net core 2.1 Web App not rendering correctly on IIS

Running .net application next to asp core app on IIS

how to set app pool identity for ASP.NET app deployed to aws elastic beanstalk

Integration test with IOptions<> in .NET Core

Integration Testing with ASP.NET Core and Entity Framework Core - How to Restore Test Data in Database Per Test?

ASP .NET Core Identity SignInManager

IIS application pool error while debugging asp.net core 3 web api

Need to configure SMTP settings on IIS for the ASP.Net Core Identity server for the EMail verification