question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get or Set cookie in application service from angular template not working.

See original GitHub issue

Hi i want to add some value to cookie when user is not logged in and want to get it back when needed. I added below code to application service and injected IHttpContextAccessor. When i call below methods from angular side cookie value returns null. Cant i use IHttpContextAccessor from application service? What would be the problem? Thx.

 [AbpAllowAnonymous]
        public void SetCookie(string value)
        {
            _httpContextAccessor.HttpContext.Response.Cookies.Append(
                  CookieKey,
                  value,
                  new CookieOptions
                  {
                      Expires = Clock.Now.AddDays(1)
                  }
              );
        }


        [AbpAllowAnonymous]
        public string GetCookie()
        {
            var cookie = _httpContextAccessor.HttpContext.Request.Cookies[CookieKey];
            return cookie;
        }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
muhlisataccommented, Mar 17, 2020

Thank you @demirmusa and @maliming . I will change my implementation. Will create a guid on client side and send it to server whenever i need it. And return bool if i should delete cookie.

0reactions
demirmusacommented, Mar 16, 2020

Using cookies to transfer data to server-client is not recommended way in angular since your angular application and server most probably be located in separate domains. Your web browser will not send them automatically because they are not located in the same domain. I recommend you get your data from the server and store it in the local-storage/cookie manually. Then you can pass it to the server (for example in the header) when it is needed.

If you need that data in all requests, you can create an interceptor and put that data to all requests header. It is what we do. Check that: https://github.com/aspnetboilerplate/abp-ng2-module/blob/6ff9cb472da1cfef562f4f84b2359f62414a8b46/src/abpHttpInterceptor.ts#L297-L301

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular is not sending the Cookie received in Set ...
For cookie based authentication, my server sends Set-Cookie to my Angular application. However, the application doesn't send the value back in ...
Read more >
Template type checking
Just as TypeScript catches type errors in your code, Angular checks the expressions and bindings within the templates of your application and can...
Read more >
Get data from a server
After installing the module, the application makes requests to and receive responses from the HttpClient . The application doesn't know that the In-memory ......
Read more >
Component interaction
This cookbook contains recipes for common component communication scenarios in which two or more components share information. See the live example ...
Read more >
Add services
This tutorial simulates getting data from the server with the RxJS of() function. Open the HeroService file and import the Observable and of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found