Issue
I have a .NET 6 Web API application that uses JWT authentication. The front end is an angular application. When I make an API call with the bearer token in the header, it returns the 401 error code. But with the same bearer token, I am able to call the same API from Swagger and get the expected data back.
I am using HttpClient
to make the API calls:
import { HttpClient, HttpHeaders } from '@angular/common/http'; ... constructor(private httpClient: HttpClient private router: Router) { } ... const tokenHeader = new HttpHeaders({ "Authorization": `Bearer ${token}` }); const options = { headers: tokenHeader }; return this.httpClient.post<any>(url, postParams, options);
request:
Preflight request:
Solution
This works for me... https://stackoverflow.com/a/55764660/10931383
Move the app.UseCors()
before these:
app.UseHttpsRedirection() app.UseDefaultFiles() app.UseStaticFiles() app.UseCookiePolicy()
Answered By - Ben Phung
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.