Get or Set cookie in application service from angular template not working.
See original GitHub issueHi 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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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