0

I am trying to understand why the API cannot see what assignments I am making in the middleware of the Website.

public async Task InvokeAsync(HttpContext httpContext) { try { httpContext.Request.Headers.SetCookie = new StringValues("some cookie"); } catch() { } }

When I pick up the API call with HttpContextAccessor it never has the values in the website assigned.

What are possible causes of this? I just don't get it.

The web has the values in the HttpRequest when it leaves the method.

Please help me understand how I can send a value to the API from the web.

Thanks in advance.

1 Answer 1

0

In ASP.NET Core middleware, using httpContext.Request.Headers.SetCookie to assign a value does not effectively set the cookie. This is because cookies are transmitted to the client via the response's Set-Cookie header, not received via the request's Set-Cookie header.

You could follow these steps to set and get the cookie:

1.Set cookies in the middleware:

httpContext.Response.Cookies.Append("MyCookie", "cookie_value");

This will add a cookie named 'MyCookie' in the Set-Cookie header of the HTTP response, which will then be sent to the client. The browser will store this cookie and automatically send it back to the server in subsequent requests.

2.Retrieve cookies sent by the client:

var cookieValue = _httpContextAccessor.HttpContext.Request.Cookies["MyCookie"];

3.Test: enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

These cookies are always the same. If I login as person X, that cookie is there and everything is great. However, when I login as person Y from a different machine, the cookies that are produced in the API are person X. Why are they "stale" or the old ones? They are not refreshing or updating. The middleware is populating them with the new values on every request.
“These cookies are always the same.” what the cookies are? The one you set in the middleware, or something else?
If I set the cookies in the web project, like you have above, they never make it to the API. They cannot be placed in the header, cookies, header cookies, response, or request. They never make it. The cookies that stay the same are the ones that are assigned during sign in. Once the user signs in, I place cookies in the response. They always stay the same for all users, not just the user that logged in and had those cookies assigned. Is there a placement in the Services of the service that would cause the cookies to not be appended? Like services.addauthentication?
public Task Invoke(HttpContext context) { context.Response.OnStarting(() => { context.Response.Headers.TryAdd("Key", "BogusValue"); return Task.CompletedTask; }); return _delegate(context); } doesn't fail, and creates the header in this method. I am trying headers, cookies, I don't care. I just want to send the API a value. How is that failing? bug was looking promising, like it already sent. But my code should account for that. I am shocked that nobody else is having this issue?
“If I set the cookies in the web project, like you have above, they never make it to the API.” Have you configured CORS in your API application, using AllowCredentials() to allow credentials to be sent from the origin? Could you show the sample demo that can reproduce the issue?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.