0

I've seen similar threads to this issue, but I had no luck solving it.

I'm trying to access my .net core controller "GET" using angular post.

getAll() {
    return this.http.get('api/Users/GetAll', this.jwt()).map((response: Response) => response.json());
} 

I'm getting this error :

401 Unauthorized

jwt code :

private jwt() {
    // create authorization header with jwt token
    let currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}');
    if (currentUser && currentUser.token) {
        let headers = new Headers({ 'Content-Type': 'application/json' });
        headers.append('Authorization', 'Bearer ' + currentUser.token)
        return new RequestOptions({ headers: headers });
    }
}

currentUser.token consist a valid token, I can access it via console.log, etc.

What am I doing wrong?

Waleed Iqbal
  • 1,308
  • 19
  • 35
user3465096
  • 3,015
  • 2
  • 12
  • 12
  • try [postman](https://www.getpostman.com/postman). See if your code has issues or request is actually unauthorised. see [this](https://stackoverflow.com/a/34465070/3331861) – Nilay Vishwakarma Dec 14 '17 at 11:41
  • postman also says the same - Unauthorized – user3465096 Dec 14 '17 at 12:12
  • I don't know any about JWT toke, but in https://blogs.msdn.microsoft.com/webdev/2017/04/06/jwt-validation-and-authorization-in-asp-net-core/ Jeffrey T. Fritz write 'bearer' (in lowecase). Sorry if it's a stupid observation – Eliseo Dec 14 '17 at 20:39
  • Check your logs of the .NET Core application. I assume you use the `Microsoft.AspNetCore.Authentication.JwtBearer` library. This should log (if you setup [logging](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x) correctly (for example `AddDebug()` for VS Output logging). There you should be able to see why JWT is invalid according to your 'back-end'. – ErazerBrecht Jan 26 '18 at 19:29

0 Answers0