You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular 11中禁用powerbi-client-angular 2.0.0的非必要Cookie

解决PowerBI嵌入时禁用ai_session/ai_user Cookie的方案

针对你使用Angular 11 + powerbi-client-angular 2.0.0遇到的PowerBI自动添加ai_session/ai_user Cookie的问题,以下是几种可行的解决机制:

1. 通过PowerBI嵌入配置禁用遥测(官方推荐)

ai_session/ai_user Cookie属于PowerBI的Application Insights遥测功能生成的,可直接通过嵌入配置关闭遥测来禁用这些Cookie:

在你的Angular组件中,配置PowerBI嵌入参数时添加telemetryEnabled: false:

// 组件内的嵌入配置
embedConfig = {
  type: 'report',
  id: '<你的报表ID>',
  embedUrl: '<你的嵌入URL>',
  accessToken: '<访问令牌>',
  settings: {
    telemetryEnabled: false // 禁用遥测,阻止生成ai_session/ai_user Cookie
  }
};

该方案是官方支持的原生配置,能从根源上避免这些Cookie被创建。

2. 使用Angular HTTP拦截器拦截并移除Cookie

如果上述方案未生效,可通过Angular的HTTP拦截器捕获PowerBI相关请求,手动移除请求头中的Cookie:

  1. 创建HTTP拦截器:
import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class PowerBiCookieInterceptor implements HttpInterceptor {

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    // 匹配目标PowerBI API地址
    if (request.url.includes('https://powerbi-df.analysis-df.windows.net/undefined')) {
      // 克隆请求并移除Cookie头
      const modifiedRequest = request.clone({
        headers: request.headers.delete('Cookie')
      });
      return next.handle(modifiedRequest);
    }
    return next.handle(request);
  }
}
  1. 在AppModule中注册拦截器:
import { NgModule } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { PowerBiCookieInterceptor } from './powerbi-cookie.interceptor';

@NgModule({
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: PowerBiCookieInterceptor,
      multi: true
    }
  ]
})
export class AppModule { }

此方法会拦截指定API的请求,强制移除Cookie头,阻止非必要Cookie的传递。

3. 手动清除已生成的Cookie(兜底方案)

如果前两种方案无法完全覆盖,可在页面加载或PowerBI嵌入完成后,手动清除已生成的ai_session和ai_user Cookie:

// 在组件的ngOnInit或嵌入成功回调中执行
clearPowerBiAICookies(): void {
  document.cookie = "ai_session=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.analysis-df.windows.net;";
  document.cookie = "ai_user=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.analysis-df.windows.net;";
}

注意需匹配Cookie的domain和path参数,确保清除生效。

内容的提问来源于stack exchange,提问作者Abhishek Ramane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 21:33:23