How to get cookie value from response using angular

I currently work on a project using .Net Core 3.1 for server and angular for the client.

I`m storing my values in a session using .net core session, now when I send a post request using postman I can see the key the response sent me back under Cookies tab (the name is .AspNetCore.Session), also when I send the request from my client I see in the network tab the desirable response under Response Headers but when I try to return it as an Observable it returns as null. enter image description here

Now my question is how can I access that set-cookie from the client? I tried some approaches and none was good.

my current startup.cs ConfigureServices:

        {              services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);             services.AddCors();                      services.AddDistributedMemoryCache();              services.AddControllers().AddJsonOptions(options =>             {                 options.JsonSerializerOptions.IgnoreNullValues = true;             });                          services.AddHttpContextAccessor();             services.AddScoped<IFavoriteRepository, FavoriteRepository>();             services.AddSession(options =>             {                 options.IdleTimeout = TimeSpan.FromSeconds(1800);                 options.Cookie.HttpOnly = true;                 options.Cookie.IsEssential = true;             });          } 

session methods

public void AddRepo(Repository repo)         {              var repository = new Repository() { RepoName = repo.RepoName, AvatarUrl = repo.AvatarUrl };             var json = JsonConvert.SerializeObject(repository);              _httpContextAccessor.HttpContext.Session.SetString("SessionUser", json);                      }          public Repository GetRepo()         {              var sessionUser = JsonConvert.DeserializeObject<Repository>(_httpContextAccessor.HttpContext.Session.GetString("SessionUser"));             return sessionUser;         } 

controller:

 [HttpPost]         public IActionResult AddFavorite(Repository repo)         {             _repo.AddRepo(repo);             return Ok();         } 

angular service

saveRepo(repo: Repository) {      return this.http.post<any>(this.baseUrl + "Repositories", repo ,{ observe: 'response'})       .pipe(         map(response => {           return response.headers;         })).subscribe(data => console.log(data));  } 
Add Comment
1 Answer(s)

Making the cookie httpOnly prevents the script from accessing it. Try deleting this line

options.Cookie.HttpOnly = true; 

(or altering it to …= false;).

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.