title: How to properly use ISession and session cookie in angular - asp.net.core app
body: <p>I'm middle in creating web app with frontend in Angular 7 and backend in ASP.NET.Core 2.1. On the server side I want to implement a simple session functionality. So in Startup.cs I configure session by using <code>AddDistributedMemoryCache()</code>, <code>AddSession()</code> and <code>UseSession()</code>. In server domain everything works fine. I have my session cookie and I have access to the session from controllers, repos etc. 
  Problem is when I call a html method from another domain (from front in Angular). In HtmlRequest there is no info about cookie session so <code>HttpContext.Session.Id</code> is always different. Can I send value of My.Cookie to front to create cookie on front side by ngx-cookie-service? Then this cookie will be send automatically to backend in HtmlRequest. How I can access to this cookie from backend? I can only get cookie from request and request from frontend doesn't have My.Cookie. Or exchange My.Cookie between front and backend should happen automatically?</p>
  
  <p>For now I created my own static storage as singleton service but I think it should be better solution with use <code>ISession</code>.  </p>
  
  <p>Startup.cs</p>
  
  <pre><code>public class Startup
      {
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddMvc();
              services
                  .AddCors(options =&gt;
                  {
                      options.AddPolicy("MyCorsPolicy", policyBuilder =&gt;
                      {
                          policyBuilder
                          .AllowAnyOrigin()
                          .AllowAnyMethod()
                          .AllowCredentials()
                          .AllowAnyHeader();
                      });
                  })
                  .AddDistributedMemoryCache()
                  .AddSession(options =&gt;
                  {
                      options.IdleTimeout = TimeSpan.FromHours(1);
                      options.Cookie = new CookieBuilder { Name = "My.Cookie", HttpOnly = false, IsEssential = true, SameSite = SameSiteMode.None };
                  });
          }
  
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              app
                  .UseCors("MyCorsPolicy")
                  .UseSession()
                  .UseMvc(routes =&gt;
                  {
                      routes.MapRoute("default",
                          "{controller}/{action}",
                          new { controller = "Home", action = "Index" });
                  });
          }
      }
  </code></pre>
  
  <p>FooController.cs</p>
  
  <pre><code>    [Route("api/[controller]")]
      [ApiController]
      public class FooController : ControllerBase
      {
          [HttpGet]
          public IActionResult FooGet()
          {
              return new OkObjectResult(new { message = HttpContext.Session?.GetString("Message") ?? string.Empty, sessionId = HttpContext.Session.Id });
          }
  
          [HttpPost]
          public IActionResult FooPost(dynamic clientSite)
          {
              string message = clientSite["message"];
              HttpContext.Session.SetString("Message", message);
              return new OkObjectResult(true);
          }
      }
  </code></pre>
  
  <p>rest.server.ts</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Observable } from 'rxjs';
  import { HttpClient } from '@angular/common/http';
  
  @Injectable()
  export class RestServer {
    private url = `http://${location.hostname}:60000\\api\\foo`;
  
    constructor(private http: HttpClient) { }
  
    public GetFoo(): Observable&lt;any&gt; {
      return this.http.get(this.url);
    }
  
    public PostFoo(message: string): Observable&lt;any&gt; {
      return this.http.post(this.url, {message});
    }
  }
  </code></pre>