C
C#8mo ago
heloo

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
Alex
Alex8mo ago
Are you running your project with http or https? Cookies with secure options will be return only in https
heloo
helooOP8mo ago
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
Alex
Alex8mo ago
refreshToken=iqK2hLxa%2FZLpUODCk7mFfVDVMnx%2BWNJS7amsLKr1NbVlgKVwseXHuDwu92N3QCxcT0LMDbmwe7e5%2BpW2zrah2g%3D%3D; expires=Tue, 04 Jun 2024 17:40:02 GMT; path=/; samesite=strict; httponly
refreshToken=iqK2hLxa%2FZLpUODCk7mFfVDVMnx%2BWNJS7amsLKr1NbVlgKVwseXHuDwu92N3QCxcT0LMDbmwe7e5%2BpW2zrah2g%3D%3D; expires=Tue, 04 Jun 2024 17:40:02 GMT; path=/; samesite=strict; httponly
Isn't it cookie that you set? I tried your code and I can see cookie in the browser
heloo
helooOP8mo ago
must be a client issue then this is hell
Alex
Alex8mo ago
What do you use as client? You need to set "includes" or "withCredentials" to true
heloo
helooOP8mo ago
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
Alex
Alex8mo ago
Sorry, but I don't know much about it
heloo
helooOP8mo ago
FIXED it was the clients fault thanks for trying
Want results from more Discord servers?
Add your server