3

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" />
Community
  • 1
  • 1
crokusek
  • 5,345
  • 3
  • 43
  • 61
  • 1
    http://stackoverflow.com/questions/28427675/correct-way-to-use-httpcontext-current-user-with-async-await (this might be helpful) – Sami Jul 13 '16 at 01:09
  • 1
    How does this function get called? From a controller? – Stephen Zeng Jul 13 '16 at 01:10
  • Looks like the sample project calls it from two places: 1) ApplicationSignInManager, 2) Startup.Auth.cs Cookie setup, OnValidateIdentity event handled by SecurityStampValidator regenerateIdentity (really should see the code). – crokusek Jul 13 '16 at 01:23
  • interesting.. context is null only at Login. But when you register, proper context available in same method – YK1 Jul 13 '16 at 14:47
  • It probably has more to do with the code path leading to this method than anything else. Note in the link that Sami provided that if a method uses `.ConfigureAwait(false)` and then calls into this code (directly or indirectly), then HttpContext is going to be null. – StriplingWarrior Dec 14 '17 at 18:01

0 Answers0