3

I am using angular 2 (not latest as using through angular-cli: 1.0.0-beta.11-webpack.9-4) and I have to set withCredentials to true for every http request. I tried to set it up for one request using

http.get('http://my.domain.com/request', { withCredentials: true })

and everything is working fine however I am trying to use something in bootstrap as below but not getting any success

import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';
import {Http, Headers, BaseRequestOptions, RequestOptions, BrowserXhr} from '@angular/http';

if (environment.production) {
  enableProdMode();
}

export class MyRequestOptions extends BaseRequestOptions {
  constructor () {
    super();
    this.headers.append('withCredentials','true');
  }
}

platformBrowserDynamic().bootstrapModule(AppModule, 
  [{ provide: RequestOptions, useClass: MyRequestOptions}]
);
Jay
  • 299
  • 1
  • 5
  • 20

2 Answers2

3

I don't know if it is required anymore, but I had the same problem and I've solved in this way:

  1. Create a subclass of BaseRequestOptions (which extends RequestOptions):

    import { Headers, BaseRequestOptions } from "@angular/http";
    
    export class AuthRequestOptions extends BaseRequestOptions {
       constructor() {
          super();
          this.withCredentials = true;
       }
    }
    
  2. Register it in application bootstrap

    import { RequestOptions } from '@angular/http';
    import { AuthRequestOptions } from './<path>/AuthRequestOptions';
    
    @NgModule({
       bootstrap: [...],
       imports: [...],
       providers: [
          { provide: RequestOptions, useClass: AuthRequestOptions},
          ...
       ]
    }...
    

(In my case, this is that worked with CORS+NTLMAuthentication)

Ahmed Musallam
  • 9,523
  • 4
  • 27
  • 47
Sierrodc
  • 845
  • 6
  • 18
  • In case you are using Angular 4.3+ with HttpClientModule, refer to this article: https://spikesapps.wordpress.com/2017/08/04/how-to-implement-windows-authentication-in-an-angular-4-3-1-application-with-a-stand-alone-web-api/ – Sierrodc Nov 07 '17 at 09:26
0

As indicated by Sierrodc Angular 4.3+ allows interceptors usage to configure this. A full example that works with NTLMAuthentication is shown below:

The interceptor

@Injectable()
export class WithCredentialsInterceptor implements HttpInterceptor {


    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        request = request.clone({
            withCredentials: true
        });

        return next.handle(request);
    }
}

Module configuration (the interceptor allows stacking with other interceptors):

@NgModule({
    imports: [CommonModule, BasicRoutingModule, HttpClientModule],
    declarations: [HomeComponent, CounterComponent, FetchDataComponent],
    providers: [
        {
            provide: HTTP_INTERCEPTORS,
            useClass: WithCredentialsInterceptor,
            multi: true
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: BaseUrlInterceptor,
            multi: true
        }
    ]
})
export class BasicModule { }

Actual usage

constructor(
    private http: HttpClient) {

    this.http.get<WeatherForecast[]>('api/SampleData/WeatherForecasts')
        .subscribe(result => {
            this.forecasts = result;
        },
        error => {
            console.error(error);
        });
}
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164