Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 无法发送授权标头_Angular_Jwt_Jwt Auth - Fatal编程技术网

Angular 无法发送授权标头

Angular 无法发送授权标头,angular,jwt,jwt-auth,Angular,Jwt,Jwt Auth,我尝试使用Angular 8 HttpClient和auth0 Angular jwt向.NET core 3发送带有授权标头的请求,但未设置标头。 服务代码 应用程序模块配置代码 我认为您不需要外部库来满足您的需要 您更应该关注拦截器的概念: 例如: import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { LocalStorageService, SessionStorag

我尝试使用Angular 8 HttpClient和auth0 Angular jwt向.NET core 3发送带有授权标头的请求,但未设置标头。 服务代码

应用程序模块配置代码
我认为您不需要外部库来满足您的需要

您更应该关注拦截器的概念:

例如:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  constructor(private localStorage: LocalStorageService, private sessionStorage: SessionStorageService) {}

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

    const token = this.localStorage.retrieve('authenticationToken') || this.sessionStorage.retrieve('authenticationToken');
    if (!!token) {
      request = request.clone({
        setHeaders: {
          Authorization: 'Bearer ' + token
        }
      });
    }
    return next.handle(request);
  }
}
从'@angular/core'导入{Injectable};
从“rxjs”导入{Observable};
从“ngx webstorage”导入{LocalStorageService,SessionStorageService};
从'@angular/common/http'导入{HttpInterceptor,HttpRequest,HttpHandler,HttpEvent};
@可注射()
导出类AuthInterceptor实现HttpInterceptor{
构造函数(专用localStorage:LocalStorageService,专用sessionStorage:SessionStorageService){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
const token=this.localStorage.retrieve('authenticationToken')| | this.sessionStorage.retrieve('authenticationToken');
如果(!!令牌){
request=request.clone({
集合标题:{
授权:“持票人”+代币
}
});
}
下一步返回。处理(请求);
}
}