Read Http Request into Byte array

The simplest way is to copy it to a MemoryStream – then call ToArray if you need to. If you’re using .NET 4, that’s really easy: MemoryStream ms = new MemoryStream(); curContext.Request.InputStream.CopyTo(ms); // If you need it… byte[] data = ms.ToArray(); EDIT: If you’re not using .NET 4, you can create your own implementation of … Read more

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

HttpContext.Current not Resolving in MVC 4 Project

Try prefixing it with System.Web. If I try System.Web.HttpContext.Current, then Current is there, but if I try HttpContext.Current, then it doesn’t recognize ‘Current’. I do have System.Web in my using statements, but I still seem to be required to specify it in order to get access to ‘Current’. @Chris’ answer points and explains the underlying … 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