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

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

accessing HttpContext.Request in a controller’s constructor

instead of putting your HttpContext.Request.IsAuthenticated in Controller level you should put it in Controller Base class that will be inherited in all of your controller with an override method of OnActionExecuting() method. In your Controller base you should have public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext ctx) { base.OnActionExecuting(ctx); ViewData[“IsAuthenticated”] = HttpContext.Request.IsAuthenticated; … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)