the domain for localhost is "". in any case it does not include protocol or port.
create cookies using the addCors service in Api project
I am trying to create a cookie.
I Run asp net core on host port: 44332
and the the front-end reactjs on port 3000
my Cors service:
services.AddCors(options =>
{
options.AddPolicy(name: "ReactChat",
builder =>
{
builder.AllowAnyMethod()
.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowCredentials();
});
});
I have tried a couple of things
string newGuid = Guid.NewGuid().ToString();
HttpContext.Response.Cookies.Append("Login", newGuid, new CookieOptions
{
SameSite = SameSiteMode.None,
Domain = "http://localhost:3000/"
}); ;
and in the configureservices:
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Domain = "http://localhost:3000";
options.Cookie.Name = ".AspNet.SharedCookie";
options.Cookie.Path = "/";
});
4 answers
Sort by: Most helpful
-
-
osyris 236 Reputation points
2021-08-03T19:36:57.1+00:00 the domain for localhost is "". in any case it does not include protocol or port.
I have tried :
HttpContext.Response.Cookies.Append("Login", newGuid, new CookieOptions { SameSite = SameSiteMode.None, Domain = "" }); ;
and this:
services.ConfigureApplicationCookie(options => { options.Cookie.Domain = ""; options.Cookie.Name = ".AspNet.SharedCookie"; options.Cookie.Path = "/"; });
both this not work:
this is my entire ConfigureServices maybe it can help to understand the problem beter:
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Database"))); services.AddIdentity<ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.Configure<IdentityOptions>(options => { // Default Password settings. options.Password.RequireDigit = true; options.Password.RequireLowercase = true; options.Password.RequireNonAlphanumeric = true; options.Password.RequireUppercase = true; options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 1; }); services.ConfigureApplicationCookie(options => { options.Cookie.Domain = ""; options.Cookie.Name = ".AspNet.SharedCookie"; options.Cookie.Path = "/"; }); services.AddCors(options => { options.AddPolicy(name: "ReactChat", builder => { builder.AllowAnyMethod() .WithOrigins("http://localhost:3000") .AllowAnyHeader() .AllowCredentials(); }); }); services.AddSignalR(); }
and the controller api:
[HttpPost("login")] public async Task<IActionResult> Login(LoginDto dto) { string newGuid = Guid.NewGuid().ToString(); HttpContext.Response.Cookies.Append("Login", newGuid, new CookieOptions { SameSite = SameSiteMode.None, Domain = "http://localhost:3000/" //HttpOnly = false }); ; return Ok(); }
-
Bruce (SqlWork.com) 55,601 Reputation points
2021-08-03T19:56:17.807+00:00 the following is still wrong.
HttpPost("login")]
public async Task<IActionResult> Login(LoginDto dto)
{
string newGuid = Guid.NewGuid().ToString();
HttpContext.Response.Cookies.Append("Login", newGuid, new CookieOptions
{
SameSite = SameSiteMode.None,
Domain = "http://localhost:3000/"
//HttpOnly = false
}); ;
return Ok();
}also what does not work mean? did you use the browser tools to trace the cookies?
-
osyris 236 Reputation points
2021-08-06T09:20:15.017+00:00 Can someone help me please solving this problem
to connect asp net core with reactjs with Cors:I have tried this code as well in the start.cs:
var directory = new DirectoryInfo("C:/Users/Gebruiker/Desktop/React/Real-Time-App/client/"); services.AddDataProtection() .PersistKeysToFileSystem(directory) .SetApplicationName("SharedCookieApp"); services.ConfigureApplicationCookie(options => { options.Cookie.Domain = ""; options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.None; options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None; });