benbaran / adal-angular4

Angular 4/5/6/7 ADAL Wrapper
MIT License
86 stars 104 forks source link

Token missing #88

Open tsegato opened 6 years ago

tsegato commented 6 years ago

I followed this step by step: http://www.digital-moves.eu/2018/07/19/authentication-with-azure-ad-angular-6-client-web-api/

I get authenticated, I can see the token in the adal object, and everything looks fine.

When I call: this.httpClient.get("http://localhost:63126/api/values");

I get following in Fiddler: GET http://localhost:63126/api/values HTTP/1.1 Host: localhost:63126 Connection: keep-alive Accept: application/json, text/plain, / Origin: http://localhost:4200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36 Referer: http://localhost:4200/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9

Following I get an 401 exception from the API. Shouldnt the library inject the token?

My module is shown below:

import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { FormsModule } from '@angular/forms';

import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { MyComponentComponent } from './my-component/my-component.component'; import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { AuthCallbackComponent } from './auth-callback/auth-callback.component'; import { AdalService, AdalInterceptor } from 'adal-angular4'; import { LogoutComponent } from './logout/logout.component'; import { AnonymousComponent } from './anonymous/anonymous.component'; import { ApiService } from './api.service';

@NgModule({ declarations: [ AppComponent, MyComponentComponent, AuthCallbackComponent, LogoutComponent, AnonymousComponent ], imports: [ BrowserModule, HttpClientModule, AppRoutingModule, FormsModule ], providers: [AdalService, { provide: HTTP_INTERCEPTORS, useClass: AdalInterceptor, multi: true }], bootstrap: [AppComponent] }) export class AppModule { } Am i missing something?

PatPat1567 commented 5 years ago

You must create a class that implements HttpInterceptor where you will intercept and add the request header Authorization with the token.