Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Bearer Tokens to Web API via Postman

Update

I have been able to get a Bearer token using instructions from this thread

Here are the instructions in Postman:

  • Url: https://login.windows.net/[tenantname].onmicrosoft.com/oauth2/token
  • Type: POST
  • Headers: none
  • Body: form-data
  • grant_type: client_credentials
  • client_id: [client-id]
  • client_secret: [client-secret]

Bearer Token Example in Postman

However, if I send the same token in my call to a Web API endpoint, I still get back "Authorization has been denied for this request"

Why is it still not authorizing ?

End Update


I have created an ASP.Net Web API project which is protected using an organizational Azure AD instance. I have set up the tenant id, client id and secret correctly.

The Azure AD instance is the same one backing our Office 365/SharePoint instance and the idea is to create SharePoint Add-Ins which can call the services using the logged in user's context.

I am stuck at testing the API. I can call unauthorized endpoints without any issue. However, when I add the [Authorize] attribute, I always get back this response: "Authorization has been denied for this request."

As I understand it, I need to generate a bearer token and add it to my Postman request in the header (see image). After much Googling, I still have not been able to make this work.

My question is: How do I generate a bearer token for a Web API instance protected by Azure AD.

My configuration code is as below:

public void ConfigureAuth(IAppBuilder app)
        {
            app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
                    Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                    TokenValidationParameters = new TokenValidationParameters {
                         ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                    },
                });
        }

Example of calling Postman with Bearer Token

like image 287
Shailen Sukul Avatar asked Mar 07 '16 23:03

Shailen Sukul


People also ask

How do I send Bearer Token in Postman?

Bearer token The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.

How do I send a Bearer Token in REST API?

To send a GET request with a Bearer Token authorization header, you need to make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.


1 Answers

First, you can use POSTMAN to test web api protected by the Bearer token. Here's my postman screenshot:

POSTMAN sending bearer token to web api

Basically: in the request header, specify the key as "Authorization", and value as "Bearer [your token". IF you run into errors, look the headers of the response and you'll see more detailed error info.

Note, most tokens have an expiration period, you can try to verify if your token is valid. e.g. https://jwt.io/

like image 147
Legend Tech Avatar answered Sep 29 '22 03:09

Legend Tech