ASP.NET HttpContext is null in 4.6.1 sample projec

2019-05-18 06:43发布

问题:

1) Create new MVC app targeting 4.6.1 (not core, using MSVS 2015 Update 3)

2) In the generated class ApplicationUser.cs add an assertion:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
     HttpContext httpContext = HttpContext.Current;
     Debug.Assert(httpContext != null);  // assertion fails
     // ...
}

3) Run program in the debugger, login, the assertion fires.

This workaround fixes the problem however I was told that HttpContext should be available across async/await as of .NET 4.5. Should I need the workaround?

Web.Config:

 <httpRuntime targetFramework="4.6.1" />