Skip to content

HttpClient.PostAsync() send Get requests #10176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
olkuziv opened this issue May 11, 2019 · 6 comments
Closed

HttpClient.PostAsync() send Get requests #10176

olkuziv opened this issue May 11, 2019 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@olkuziv
Copy link

olkuziv commented May 11, 2019

Describe the bug

After adding [Authorize] attribute on Post method, aftert calling HttpClient.PostAsync() working like GetAsync().

To Reproduce

Steps to reproduce the behavior:

  1. Create default blazor app
  2. Add Coockies auth
  3. Add new Get endpoint
  4. Add [Authorize] attribute for new Post endpoint
  5. Call this Post endpoint
  6. In debug could see that HttpClient send Get request

Screenshots

image

image

@olkuziv
Copy link
Author

olkuziv commented May 11, 2019

After adding handler
image
PostAsync returns "Found 302" status code, but endpoid still not working.

@dcarr42
Copy link

dcarr42 commented May 11, 2019

There is certainly some weirdness here. What are you expecting to be returned from the endpoint?

Regards to the response with the auth challenge, that's normal.
You may need to reconsider how you initiate the auth flow with respect to the blazor client. There will be developments in this area so you may need to consider future best practices.

#4048

@enetstudio
Copy link

@olkuziv, HttpClient is automatically added to your application as a Singleton service. The HttpClient is based on the WebAssemblyHttpMessageHandler. Why do you add HttpClient as a scoped service, and based on HttpClientHandler ? I'm not suggesting that it is related to your issue...just wondering why you coded that way ?

@enetstudio
Copy link

@Eilon Eilon added the area-blazor Includes: Blazor, Razor Components label May 13, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @olkuziv.
You're most probably being auto-redirected by the server to the authentication endpoint, which is the expected behavior when using the CookieAuthentication. So your HttpClient automatically sends a get request to the authentication endpoint, and that's what you're observing here. To successfully make the request, make sure that the user is authenticated and you pass the cookie as part of the first request.

@olkuziv
Copy link
Author

olkuziv commented May 16, 2019

Thanks for contacting us, @olkuziv.
You're most probably being auto-redirected by the server to the authentication endpoint, which is the expected behavior when using the CookieAuthentication. So your HttpClient automatically sends a get request to the authentication endpoint, and that's what you're observing here. To successfully make the request, make sure that the user is authenticated and you pass the cookie as part of the first request.

Yes, You are right. I should pass cookies in HttpClient headers

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

5 participants