Postman gets a valid token but getting 401 from API

a 夏天 提交于 2020-03-26 04:29:07

问题


I am trying to test my ASP.NET Core 2.2 Web API with Postman. My API is protected by Azure AD. I have been using the "Web App your API" Microsoft Azure Sample on GitHub and triple-checked all of the steps.

Using Postman I ask it to get a token using the OAuth2 Implicit flow and I do get a token returned. If I decode the token it seems to contain all the claims I want: aud, upn and scp all make sense.

However, when I then send a request to the API using the returned token I still get a 401.

The API is working because if I remove the [Authorize] attribute I get data returned.

If I try accessing the API with a web client I get an error: MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call. but that makes no sense given that my upn is returned in the token.

Can anyone recommend further action to figure out what's wrong.

UPDATE

On line 381 of TokenAcquisition.cs account is null so when AcquireTokenSilent() is subsequently called the result is null.


回答1:


I abandoned Postman for a while and focused on a web client. I had to rename my Application ID URI in the Azure Portal from https://<company>.onmircrosoft.com/<myapi>/user_impersonation to api://<application_guid>/access_as_user and similarly in my application code. This made TokenAcquisition.cs start returning a token and I got the Web App talking to the API.

I still don't have Postman working though now it returns a message: invalid_client. Probably as I need to use a different flow. Anyhow, I'm happy for the moment.



来源:https://stackoverflow.com/questions/57978358/postman-gets-a-valid-token-but-getting-401-from-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!