Angular 如何在canActive函数中获取标题值?

Angular 如何在canActive函数中获取标题值?,angular,typescript,rxjs,Angular,Typescript,Rxjs,我需要重定向到不同的用户页面,这取决于从标题接收到的userRole值 角度布线 最初我将重定向到LoginComponent, CanActive random.guard.ts是一个API调用,用于从服务器获取标头详细信息 random.guard.ts loginservice.ts 如果我订阅http get调用,我将获得头值。如何重构代码并接收头值。在后端,我正在使用Web API核心,请查看以下API: [HttpGet] [Route("admins/overview/{id}")

我需要重定向到不同的用户页面,这取决于从标题接收到的userRole值

角度布线

最初我将重定向到LoginComponent, CanActive random.guard.ts是一个API调用,用于从服务器获取标头详细信息

random.guard.ts

loginservice.ts


如果我订阅http get调用,我将获得头值。如何重构代码并接收头值。

在后端,我正在使用Web API核心,请查看以下API:

[HttpGet]
[Route("admins/overview/{id}")]
public IActionResult GetOverview(int id)
{
    var item = _adminService.GetOverviewById(id);
    Response.Headers.Add("Roles","Admin,User,Editor");// Here we add the roles with its values to Header
    Response.Headers.Add("Access-Control-Expose-Headers", "Server,Roles"); // specify the name of headers to access
    return Ok(item);
}
在这里,我添加了两个头:第一个是角色及其值,第二个是访问控制中的暴露头,其名称是我们希望在客户端访问的头,它们是服务器角色

默认情况下,仅显示6个CORS安全列出的响应头:

Cache-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
现在,您可以在Angular中访问它们

您可以观察完整响应,为此,必须将observe:response传递到options参数中

试试这个:

isHeader(): Observable<boolean> {
    return this.http.get(`${environment.baseUrl}home/login`,{observe: 'response', withCredentials: true}).pipe(
        map((response: any) => {
        // Here, resp is of type HttpResponse<Sth>.
        // You can inspect its headers:
           console.log(resp.headers.get('roles')); <-- Get Value of Roles
        // And access the body directly, which is typed as MyJsonData as requested.
           console.log(resp.body.someField);
        })
    );
}
看到这个->


和->

为什么该信息必须在标题中?难道这不能成为回应的一部分吗?顺便说一句,你实际上可以使用HTTP拦截器来访问标题;我现在不使用拦截器。如果我使用拦截器会解决这个问题吗?@Rijo-Yep。不要在头中获取用户角色,只需将其作为响应返回即可。应用程序首先通过此API调用进行通信。当我触发这个API调用时,它将通过springboot过滤器服务传递。筛选器服务将提供此标头详细信息。所以他们什么也做不了。你查过了吗?resp.headers.get'X-Custom-Header'未定义,此时无法访问头值point@Rijo-数小时后,我意识到在后端,我们应该指定要在客户端访问的头,例如Angular。我修改了我的答案,我会检查一下,让你知道谢谢。请回答这个问题
[HttpGet]
[Route("admins/overview/{id}")]
public IActionResult GetOverview(int id)
{
    var item = _adminService.GetOverviewById(id);
    Response.Headers.Add("Roles","Admin,User,Editor");// Here we add the roles with its values to Header
    Response.Headers.Add("Access-Control-Expose-Headers", "Server,Roles"); // specify the name of headers to access
    return Ok(item);
}
Cache-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
isHeader(): Observable<boolean> {
    return this.http.get(`${environment.baseUrl}home/login`,{observe: 'response', withCredentials: true}).pipe(
        map((response: any) => {
        // Here, resp is of type HttpResponse<Sth>.
        // You can inspect its headers:
           console.log(resp.headers.get('roles')); <-- Get Value of Roles
        // And access the body directly, which is typed as MyJsonData as requested.
           console.log(resp.body.someField);
        })
    );
}
server: Kestrel
content-type: application/json; charset=utf-8
roles: Admin,User,Editor