2016-09-28 55 views
3

我使用角度2(不是最新的使用通过角度cli:1.0.0-beta.11-webpack.9-4),我必须设置withCredentials以每个http请求都为true。我试图将其设置为使用Angular 2 - 追加withCredentials =真正的每个http请求

http.get一个请求(“http://my.domain.com/request”,{withCredentials:真})

,一切工作正常不过我想下面而不是在引导使用的东西获得任何成功

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}] 
); 
+0

喜, 我面临同样的问题,你是否知道如何做到这一点? – Gustavo

+0

不,我还没有解决它... – Jay

回答

3

如果不再需要它,我不知道,但我有同样的问题,我已经用这种方式解决:

  1. 创建SUBC BaseRequestOptions的姑娘(延伸RequestOptions):

    import { Headers, BaseRequestOptions } from "@angular/http"; 
    
    export class AuthRequestOptions extends BaseRequestOptions { 
        constructor() { 
         super(); 
         this.withCredentials = true; 
        } 
    } 
    
  2. 注册它的bootstrap

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

(对我来说,这是一个以CORS + NTLMAuthentication工作)

+0

如果您使用Angular 4.3+与HttpClientModule,请参阅此文章:https://spikesapps.wordpress.com/2017/08/04/how-to-implement -Windows认证功能于一个角向-4-3-1的应用程序上带有一个-独立的Web-API / – Sierrodc