Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

"Catastrophic failure" during the startup on IIS when ApplicationInitialization module is installed and use impersonation

Open marcusien opened this issue 8 years ago • 16 comments

Hi,

We're facing a highly blocking issue. Our case is Application :

  • netcore application targetting the full framework 4.6.1 and using 2.0 packages
  • use Impersonation via an ImpersonateMiddleware

IIS Machine :

  • dotnet 2.0.5 x64 runtime
  • IIS 8.5
  • Application initialization module installed
  • Application pool has preloadEnabled=true and StartMode=AlwaysRunning

When I run the website from iisexpress, everything is fine When I run the website from IIS without the Application initialization module installed, everything is fine When I run the website from IIS with the Application initialization module installed, I got this error and trace :

System.Runtime.InteropServices.COMException (0x8000FFFF): Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) at System.Reflection.RuntimeAssembly.nLoadFile(String path, Evidence evidence) at System.Reflection.Assembly.LoadFile(String path) at Microsoft.AspNetCore.Mvc.Razor.Compilation.ViewsFeatureProvider.GetFeatureAssembly(AssemblyPart assemblyPart) at Microsoft.AspNetCore.Mvc.Razor.Compilation.ViewsFeatureProvider.GetViewAttributes(AssemblyPart assemblyPart) at Microsoft.AspNetCore.Mvc.Razor.Compilation.ViewsFeatureProvider.PopulateFeature(IEnumerable`1 parts, ViewsFeature feature) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature) at Microsoft.AspNetCore.Mvc.RazorPages.Internal.CompiledPageRouteModelProvider.GetViewDescriptors(ApplicationPartManager applicationManager) at Microsoft.AspNetCore.Mvc.RazorPages.Internal.CompiledPageRouteModelProvider.EnsureCache() at Microsoft.AspNetCore.Mvc.RazorPages.Internal.CompiledPageRouteModelProvider.OnProvidersExecuting(PageRouteModelProviderContext context) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionDescriptorProvider.BuildModel() at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context) at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.UpdateCollection() at Microsoft.AspNetCore.Mvc.Internal.ActionDescriptorCollectionProvider.get_ActionDescriptors() at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.GetTreeRouter() at Microsoft.AspNetCore.Mvc.Internal.AttributeRoute.RouteAsync(RouteContext context) at Microsoft.AspNetCore.Routing.RouteCollection.d__9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at AspNetCore.IISInitialization.ImpersonateMiddleware.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext() info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 302.7203ms 500 text/html; charset=utf-8

If the same application warmup request is done by an administrator, the website runs properly.

It seems there's a bad interaction between the impersonation process and applicationinitialization module of IIS.

For information, I made a test with the ASP Net core web template and just added the following middleware in the pipe

public async Task Invoke(HttpContext httpContext)
{
    // Basic impersonate call
    using((httpContext.User.Identity as WindowsIdentity)?.Impersonate())
    {
        await _next.Invoke(httpContext);
    }
}
app.UseStaticFiles();
// Add the impersonate middleware in the request pipeline
app.UseImpersonateMiddleware();
app.UseMvc(routes => ....

marcusien avatar Jan 17 '18 08:01 marcusien

Shouldn't that impersonate call be inside of a using?

davidfowl avatar Jan 17 '18 10:01 davidfowl

Yes, forgot to add it in my sample project, but it has no incidence with the original issue. The problem is still there

marcusien avatar Jan 17 '18 11:01 marcusien

@Tratcher, @shirhatti do you have any idea where this bug should go to?

mkArtakMSFT avatar Jan 17 '18 18:01 mkArtakMSFT

That impersonation middleware does not work, there is no async impersonation support. Impersonation must be done synchronously at the needed call site. Where did you get that sample? All known samples of that faulty code have been redacted, please take it down. See https://github.com/dotnet/corefx/issues/9996#issuecomment-307870746, https://github.com/dotnet/corefx/issues/24977

Please update your repro and we'll re-evaluate.

Tratcher avatar Jan 17 '18 18:01 Tratcher

Sorry, that was a bit rash. The .NET Core version of that middleware doesn't work because they don't have the Impersonate method, they have RunImpersonated. On .NET 4.6.1 it might work but we haven't done any testing with that pattern.

If you drop the impersonation and give the default app pool identity full read & execute access to the application dir does it work?

Tratcher avatar Jan 17 '18 18:01 Tratcher

With dropping the impersonation it works yeah. That's the problem but we need to use impersonation.

And I did not understood why you were saying that we can't use async on the Middleware.

marcusien avatar Jan 17 '18 20:01 marcusien

What do you need the impersonation for?

It looks like all impersonated users would require full read access to the application directory to compile views.

Tratcher avatar Jan 17 '18 20:01 Tratcher

We need impersonation cause we're using kerberos for authentication and we need the user credentials for accessing the database with "integrated security"

I don't know if there is a solution to send a request for warming up the pool before impersonation, waiting for the response and then continue the original request with impersonation ?

marcusien avatar Jan 17 '18 20:01 marcusien

Well, you have a couple of choices: A) Change your credential pattern for DB access. E.g. use a dedicated account rather than the end-users credentials. B) Move the impersonation to only encompass the DB calls. C) Give all of the impersonated users read access to the application directory. D) Get the views compiled and loaded outside of the impersonation context. @rynowak is this the pre-compiled view code path? Or is it trying to compile views dynamically?

Tratcher avatar Jan 17 '18 22:01 Tratcher

Well I'll tried to check some of the provided option but until the setup of "application initialization" on IIS everything worked fine no matter impersonation is called and without forcing read access for all users or stuff like that. I think there's some digging to make anyway

marcusien avatar Jan 17 '18 22:01 marcusien

Application initialization works by making a fake request, correct? What's the user on that request? Anonymous? Is it possible you're impersonating an anonymous user? Do you need to check the user type before impersonating them?

Tratcher avatar Jan 17 '18 22:01 Tratcher

There's no (or not so much?) documentation about the behavior of application initialization of IIS and dot net core. I need to make sure of it but even if we disable the preloadEnabled (which is in charge to run the fake request), we have this exception (catastrophic failure).

Is there any way to identify this fake request inside the startup or middleware ? Impersonating an anonymous user, don't think so cause we disabled the anonymous on the pool.

I'll be able to answer some of your questions tomorrow at work :)

marcusien avatar Jan 17 '18 22:01 marcusien

@marcusien

C) Give all of the impersonated users read access to the application directory.

This seems like the most reasonable thing to do no? Do you know if that's already the case?

davidfowl avatar Jan 18 '18 07:01 davidfowl

@davidfowl I am not administrator of the domain and when I'm trying to call the website, i get the error. I have full access (read-write) to the website directory (It's me who's publishing the website).

When one of my system admin try to connect after resetting the application pool, he can successfully load the website.

To sum up : In my case (I'm developer with full read-write access in the website root directory)

  1. Application pool reset
  2. I'm trying to get access to my website
  3. Catastrophic failure

In my system admin case (administrator of the whole world :))

  1. Application pool reset
  2. He's trying to get access to the website
  3. Homepage is getting displayed

marcusien avatar Jan 18 '18 07:01 marcusien

To answers the different options : A) Change your credential pattern for DB access. E.g. use a dedicated account rather than the end-users credentials. => No way, we need the user to be identified at the database level B) Move the impersonation to only encompass the DB calls. => don't have the solution. We are using EF6.1.3 for information with repository pattern C) Give all of the impersonated users read access to the application directory. => Already have D) Get the views compiled and loaded outside of the impersonation context. @rynowak is this the pre-compiled view code path? Or is it trying to compile views dynamically? => I think the view compilation and loading is done inside the MVC middleware (by using "useMvc"). I tried moving the "useImpersonationMiddleware()" after the calling of "useMvc()" but in that case, the impersonation is not used by the SQL connection.

marcusien avatar Jan 18 '18 11:01 marcusien

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.

aspnet-hello avatar Sep 24 '18 18:09 aspnet-hello