The number of cookies is zero after refreshing the page and angular routing with correspondence URL

I am using angular Spa template with .net core 2.0 and angular 4.

I get zero cookies after page refresh. and in fact I get nothing in the HttpContext after refreshing the page.

Here is my cookie in the browser. enter image description here

It works fine if I use the angular system menu.

Now here is my cookie code at the end.

private string GetCookie(HttpContext httpContext, string cookieName) { var rqstCookie = httpContext.Request.Cookies[cookieName]; return !string.IsNullOrEmpty(rqstCookie) ? rqstCookie : null; } 

The important thing is that when I click on the menu, it downloads the components and services files, and then requests the C # controller with all the headers and parameters. but when I refresh the page, this whole process is skipped, so I think that these settings are not delivered to the end and why my cookies are empty.

in my other applications, I used URL rewriting for the same problems, and it worked there, but here, because this application is angular spam template, url-rewrite does not work.

I also tried setting up the URL redirect, as in my old applications, but this gives me an error. here is the URL rewrite module link that I followed. stack overflow

I have cookies for the session cookie, but it also does not work.

Can you advise me how to get all cookies and other data, even when the user refreshes the page. thanks...

+5
source share
2 answers

This was an angular problem.

when I added APP_BASE_HREF to my routing module, it suddenly started working.

fooobar.com/questions/53386 / ...

Here is my code

 import { APP_BASE_HREF } from "@angular/common"; export const sharedConfig: NgModule = { bootstrap: [ AppComponent ], declarations: [ AppComponent, ... ], imports: [ HttpModule, ..., RouterModule.forRoot([ { path: "", redirectTo: "home", pathMatch: "full" }, { path: "**", redirectTo: "home" } ], { useHash: true }), ], providers: [ { provide: APP_BASE_HREF, useValue: '/' }, ], }; 

So, this is basically a hashing technology on the router side, and it seems that I was also absent from this provider in my application.

+3
source

try to indicate when the cookie expires using this code when writing a cookie

  option.Expires = DateTime.Now.AddDays(x); Response.Cookies.Append(cookiename, cookievalue, option); 

you can change AddDays(x) by the number of days you want

+3
source

Source: https://habr.com/ru/post/1275442/


All Articles