2017-11-19 12 views
0

Ich brauche Windows-Authentifizierung in meinem Projekt, aber Post-Anfrage ist gebrochen. Ich habe eine globale Implementierung von HttpInterceptor.Angular 5: Nach Anfrage & Windows-Authentifizierung

Cors in meinem Backend aktiviert.

Wenn ich get-Anfrage senden, erhalte ich Benutzeridentität im Backend und es ist in Ordnung. Was ist falsch mit Post-Anfrage?

OPTIONS http://localhost:56789/api/document/GetDocuments 401 (Unauthorized) 
Failed to load http://localhost:56789/api/document/GetDocuments: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401. 

P.S. Wenn ich die anonyme Authentifizierung gefasst alle bekommen/Post-Anfragen arbeiten, aber ich weiß nicht Benutzeridentität

Umwelt

Angular version: ^5.1.0-beta.1 
Angular-cli version: ^1.6.0-beta.2 

Browser: 
- [ x ] Chrome (desktop) version 62.0.3202.94 

For Tooling issues: 
- Node version: XX 8.8.1 
- Platform: Windows 

Mein Frontend Service habe:

@Injectable() 
export class DocumentService { 
    controllerName: string = 'document'; 
    constructor(private http: HttpClient, @Inject('API_URL') private apiUrl: string) { } 

    getDocuments(view: AppModels.View.BaseView): rx.Observable<AppModels.Document.DocumentList> {   
     return this.http.post<AppModels.Document.DocumentList>(`${this.apiUrl}/${this.controllerName}/GetDocuments`, view); 
    } 
} 

Mein HttpInterceptor:

@Injectable() 
export class AuthHttpInterceptor implements HttpInterceptor { 
    intercept(req: HttpRequest, next: HttpHandler): rx.Observable<HttpEvent> { 
     const headers = {}; 
     const clone = req.clone({ setHeaders: headers, withCredentials: true }) 
     return next.handle(clone); 
    } 
} 

Antwort

1

Versuchen Sie, sowohl die anonyme als auch die Windows-Authentifizierung zu aktivieren, und legen Sie [Autorisierung e] Attribut auf Ihrem Controller. Ich glaube, dass die 401 nicht autorisierte Antwort durch das Aktivieren von CORS verursacht wird und das Ergebnis davon ist, dass der Preflight-Header kein Authentifizierungstoken enthält.

Eine Alternative wäre Ihre http POST auf eine 'einfache Anfrage' zu ändern, die auf Content-Type Werte begrenzt:

  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain

'einfache Anfragen' keine CORS Preflight auslösen. Details zur CORS-Spezifikation finden Sie unter https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS.