HttpContext.Current.User.Identity.Name is always string.Empty

FormsAuthentication.SetAuthCookie(tbUsername.Text, true); bool x = User.Identity.IsAuthenticated; //true string y = User.Identity.Name; //”” The problem you have is at this point you’re only setting the authentication cookie, the IPrincipal that gets created inside the forms authentication module will not happen until there is a new request – so at that point the HttpContext.User is in a … Read more

access HttpContext.Current from WCF Web Service

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration: <configuration> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/> </system.serviceModel> </configuration> That in turn allows you to get access to the current user: HttpContext.Current.User – which is what you’re after, right? You can even enforce AspNetCompatibility by decorating your service class with an additional attribute: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] … 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

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

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

Get current System.Web.UI.Page from HttpContext?

No, from MSDN on HttpContext.Current: “Gets or sets the HttpContext object for the current HTTP request.” In other words it is an HttpContext object, not a Page. You can get to the Page object via HttpContext using: Page page = HttpContext.Current.Handler as Page; if (page != null) { // Use page instance. }

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