How do I secure a Web API with Azure AD B2C

237 Views Asked by At

I have a Angular application and a separate Web API solution built with .NET Core. I have successfully setup authentication with Azure AD B2C. I am able to login to the angular application. However I would like to secure one of my Web API calls. i.e. http://localhost/Profile/GetProfile. The trouble is that I'm able to query this url successfully even when not logged in.

I used code from the sample application in github and strangely I get 401 not authorized when trying to make my api call from my ClientApp. However, I am able to open that url successfully in a new tab (outside of my application). I am trying to achieve the opposite of this. i.e. it should 401 from a browser but 200 from my ClientApp. What am I doing wrong?

BTW the sample application throws a unhandled exception when trying to navigate to the todo menu item.

1

There are 1 best solutions below

2
On

The trouble is that I'm able to query this url successfully even when not logged in.

This is because the cache is still there after you successfully log in. Once you've used browser privacy mode, you'll need to log in again.

And is these screenshots below what you want to achieve? You can decode token in this.Whether the requested token is successful does not match the api endpoint.If so,please check your client app (API perssions) & server app(Expose an API).

  1. 401 from a browser enter image description here

2.200 from my ClientApp.(Here's a microsoft graph me/endpoint demo.) enter image description here