Correct way to use HttpContext.Current.User with async await

As long as your web.config settings are correct, async/await works perfectly well with HttpContext.Current. I recommend setting httpRuntime targetFramework to 4.5 to remove all “quirks mode” behavior. Once that is done, plain async/await will work perfectly well. You’ll only run into problems if you’re doing work on another thread or if your await code is … Read more

How do I access HttpContext in Server-side Blazor?

Add the following to Blazor.Web.App.Startup.cs: services.AddHttpContextAccessor(); You also need this in <component-name>.cshtml @using Microsoft.AspNetCore.Http @inject IHttpContextAccessor httpContextAccessor Note: At the time when this answer was written, accessing the HttpContext was done as described above. Since then, Blazor has been under rapid development, and has fundamentally changed. It is definitely deprecated the usage described above, but … Read more

HttpContext.Current.User.Identity.Name is Empty

I struggled with this problem the past few days. I suggest reading Scott Guthrie’s blog post Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application For me the problem was that although I had Windows Authentication enabled in IIS and I had <authentication mode=”Windows” /> in the <system.web> section of web.config, I was not … Read more

System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET

Description System.Web.HttpContext.Current.User.Identity.Name Gets or sets security information for the current HTTP request. (The Name of the Logged in user on your Website) System.Environment.UserName Gets the user name of the person who is currently logged on to the Windows operating system. More Information MSDN – HttpContext.User Property MSDN – Environment.UserName Property

Using HttpContext.Current in WebApi is dangerous because of async

HttpContext.Current gets the current context by Thread (I looked into the implementation directly). It would be more correct to say that HttpContext is applied to a thread; or a thread “enters” the HttpContext. Using HttpContext.Current inside of async Task is not possible, because it can run on another Thread. Not at all; the default behavior … Read more

Why is HttpContext.Current null?

Clearly HttpContext.Current is not null only if you access it in a thread that handles incoming requests. That’s why it works “when i use this code in another class of a page”. It won’t work in the scheduling related class because relevant code is not executed on a valid thread, but a background thread, which … Read more

Setting HttpContext.Current.Session in a unit test

You can “fake it” by creating a new HttpContext like this: http://www.necronet.org/archive/2010/07/28/unit-testing-code-that-uses-httpcontext-current-session.aspx I’ve taken that code and put it on an static helper class like so: public static HttpContext FakeHttpContext() { var httpRequest = new HttpRequest(“”, “http://example.com/”, “”); var stringWriter = new StringWriter(); var httpResponse = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest, httpResponse); var … Read more