Cookies help Dotnet webapi
Iam using dotnet webapi trying to set cookies in browser the request returns 200 but the cookies arent getting set
CookieOptions cookOpt = new()
{
HttpOnly = true,
Expires = refresh.ExpiresAt,
SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict,
Secure = true,
};
_httpContext.HttpContext?.Response.Cookies.Append("refreshToken", refresh.Token, cookOpt);
Console.WriteLine("set");
here is my set cookie method
8 Replies
Are you running your project with
http
or https
? Cookies with secure
options will be return only in https
tried both
removed the secure option tried http didnt work
CookieOptions cookOpt = new()
{
HttpOnly = true,
Expires = refresh.ExpiresAt,
SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict,
};
_httpContext.HttpContext?.Response.Cookies.Append("refreshToken", refresh.Token, cookOpt);
even clicked on allow on third party cookies in settings
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Sun, 05 May 2024 17:40:02 GMT
Server: Kestrel
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:3000
Set-Cookie: refreshToken=iqK2hLxa%2FZLpUODCk7mFfVDVMnx%2BWNJS7amsLKr1NbVlgKVwseXHuDwu92N3QCxcT0LMDbmwe7e5%2BpW2zrah2g%3D%3D; expires=Tue, 04 Jun 2024 17:40:02 GMT; path=/; samesite=strict; httponly
Transfer-Encoding: chunked
Vary: Origin
also heres the response header
Isn't it cookie that you set?
I tried your code and I can see cookie in the browser
must be a client issue then
this is hell
What do you use as client? You need to set "includes" or "withCredentials" to true
try {
const response = await fetch('https://url/api/Auth?Id=5', {
method:"GET",
credentials:'include',
});
// Handle response const data = await response.json() setToken(data.data); } catch (error) { // Handle error console.error('Error:', error); // You may want to display an error message to the user } }; ive manually written the cookie but it wont send the request gets sent but var res = new ServiceResponse<string>(); var Token = _httpContext.HttpContext?.Request.Cookies["refreshToken"]; if (Token is null) { res.StatusCode = 400; res.ErrorMessage = "No RefreshToken!"; return res; } and i tested this on my local server it works when i send try with my local dotnet webapi server the token gets sent but when i deploy it to a remote server the refresh tokens are null
// Handle response const data = await response.json() setToken(data.data); } catch (error) { // Handle error console.error('Error:', error); // You may want to display an error message to the user } }; ive manually written the cookie but it wont send the request gets sent but var res = new ServiceResponse<string>(); var Token = _httpContext.HttpContext?.Request.Cookies["refreshToken"]; if (Token is null) { res.StatusCode = 400; res.ErrorMessage = "No RefreshToken!"; return res; } and i tested this on my local server it works when i send try with my local dotnet webapi server the token gets sent but when i deploy it to a remote server the refresh tokens are null
Sorry, but I don't know much about it
FIXED
it was the clients fault
thanks for trying