1

I want to get a Cookie with specific name and want to check the Value of it. I've seen a lot of different suggestions, but i can not create a Request like this

bool isApproved = HttpContext.Current.Request.Cookies("myCookie").Value.Equals("true", StringComparison.OrdinalIgnoreCase) ?? false;

i am using the using:

@using Microsoft.AspNetCore.Http

but now 'Current' throws the message: httpcontext does not contain a definition for current

Why does that work for others and not form me? Am I missing something?

Thanks for your Help, Schlindibus

slideBruv
  • 80
  • 13
  • [How to get `HttpContext.Current` in .NET Core?](https://stackoverflow.com/questions/38571032/how-to-get-httpcontext-current-in-asp-net-core) [Access the current HttpContext in .NET Core](https://stackoverflow.com/questions/31243068/access-the-current-httpcontext-in-asp-net-core) – John Wu May 19 '22 at 06:32
  • @JohnWu How would I use is as an example, when im corking insie of an .cshtml-File? – slideBruv May 19 '22 at 06:44
  • Does this answer your question? [Access the current HttpContext in ASP.NET Core](https://stackoverflow.com/questions/31243068/access-the-current-httpcontext-in-asp-net-core) – jazb May 19 '22 at 06:53
  • @jazb Kind of. It makes scense to me, but I don't know how I'd solve this in a.cshtml-File. I tried doing it like this: ´IHttpContextAccessor.HttpContext.Request.Cookies´ but i get that an object reference is required for the non-static field, method or group. – slideBruv May 19 '22 at 06:57
  • [Documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-6.0) includes how to use from Razor view – John Wu May 19 '22 at 07:36

1 Answers1

0

Thanks to the comments under my Post i found how to do it. Just like @John Wu wrote, the solution get's featured in this post: Access the current HttpContext in ASP.NET Core

In this post you need to inject. Since I'm working in a .cshtml-File I figured to inject it in the File itself like this: @inject IHttpContextAccessor httpContextAccessor

Now I can search for the cookie like this:

bool isApproved = httpContextAccessor.HttpContext.Request.Cookies["Cookie.Name"] == "true";
slideBruv
  • 80
  • 13