Hi @lesponce
I do see the cookie in Postman. I don't see it in my browser's developer tool. How do see the cookie value or how do I retrieve it from another page?
From the previous discussion, we know that to access the session value in the second request or another page, when send the request we need to add the session cookie in the request header, after that we can access the session value.
In your scenario, before accessing another page via browser, I think you didn't call the webhook page, right? Because it is Post request. So, you can't see it in the browser's developer tools and then can't get the session value.
To solve this problem, you can try to change the code, to access the webhook page using the MapGet() method, then we can directly access it via browser. Or if you still want to use the MapPost method, you have to create another web page (Get) and send a post request to webhook endpoint.
app.MapGet("/webhook", async context =>
{
var requestBody = new WebhookPayload(context.Request.Query["Header"].ToString(), context.Request.Query["Body"].ToString());
//var requestBody = await context.Request.ReadFromJsonAsync<WebhookPayload>();
Console.WriteLine($"Header: {requestBody?.Header}, Body: {requestBody?.Body}");
context.Response.StatusCode = 200;
// Create a new Session
context.Session.SetString("YourSessionKey", "YourSessionValue");
var value = context.Session.GetString("YourSessionKey");
// Create a new cookie
var cookieOptions = new CookieOptions
{
// Set the cookie properties
Path = "/",
Expires = DateTime.UtcNow.AddDays(7),
Secure = true, // Use "false" if not using HTTPS
HttpOnly = true,
SameSite = (Microsoft.AspNetCore.Http.SameSiteMode)SameSiteMode.Strict
};
context.Response.Cookies.Append("myKey", "myValue", cookieOptions);
await context.Response.WriteAsync(requestBody.ToString());
});
Then, before access another page, you should access the webhook page first.
The result as below: we can see that, after access the webhook page, the cookie was added to current domain, then when send the next request (to access another page), the session cookie will add to the request header, and we can access the session value.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion