11
votes

I'm trying to test my API with Identity Server Asp.net Core using Postman.

This is the way that I'm trying to do:

  1. First request HttpGet to https://localhost:5000/Account/Login and in response body I received: <input name="__RequestVerificationToken" type="hidden" value="CfDJ8MoS9upoM4dNp8Kx-AdvA-uYr13_PAkuMZpzYMV8UmxZq5GdLTvN-Ht5NpTLmPtlhL5d5z2Hu2vUJoJGhk1AMlARDcOwqgq7Cef1dfQL_vl4tIFM4kx9RZPz8DHU26-U9qLnKAIstZgR42-1FuGNh24" />

And in Cookie (not sure for what it is though): enter image description here

  1. Then HttpPost to https://localhost:5000/Account/Login with RequestVerificationToken with token received from body HttpGet request. enter image description here

And always error 400 as you can see at screen shot above.

In Visual studio I can see that some request was catched but clearly was incorrect. enter image description here

If I'll remove attribute [ValidateAntiForgeryToken] then of course everything works fine but obviously because that validation is disabled.

3
The input field name is __RequestVerificationToken. Note the double underscore at the start. - Brad
Right, I was trying before with __ and the same problem occurs. - DiPix
Are you including it in the body? It's a form field, not a header. - Brad
Yes, I was trying body either. - DiPix
He needs it to get a Bearer Token and to be able to add these Bearer tokens to his API requests, so that he can Test Methods that have the [Authorize] Attribute on them. I have the same problem. - axelrotter

3 Answers

2
votes

You'd need to do followings to send such a request:

1.) Enter __RequestVerificationToken key value (don't forget double underscores) into x-www-form-urlencoded

2.) You need to add .AspNetCore.Antiforgery cookie to the Cookies section in Postman.

For example like this .AspNetCore.Antiforgery.1XHiLFgQI2w=your cookie value; Path=/; Domain=localhost;Expires=Session;

You can find .AspNetCore.Antiforgery cookie in Application section in Google Developer Tools

.AspNetCore.Antiforgery cookie in Google Developer Tools picture

Add cookie in Postman picture

1
votes

Just spent a lot of time on this.

I did several things:

  1. Setup an Environment and added a variable.
  2. Added a pre-request script that...
    • Uses pm.SendRequest to Get the page
    • uses cheerio to find the first input field named __RequestVerificationToken and get its value
    • set the environmental variable to the value retrieved from the field
  3. send the form data (since I'm using asp.net core, the values for the model), as x-www-form-urlencoded
  4. and last, but not least, I added __RequestVerificationToken as one of the key value pairs in the form data and set it to the use the variable already setup

The main reason I am posting this answer is the last, I saw a lot of things on the web that indicated that name was supposed to be RequestVerificationToken, and that doesn't work, just leads to a 400 response (bad request).

-1
votes

In postman, you’d need to set the content type to form url encoded.

And send the request Verification token in the header as "RequestVerificationToken"

However, if you just need a Bearer token then you need to call POST https://<your identity server>/connect/token with the enter image description here