Problem Statement

我正在开发一个SPA,用户可以在其中查看/查看pdf,text和html等不同的报告 . 因为我们分别使用授权服务器和资源服务器,每个资源服务器必须向授权服务器请求用户身份和然后授权服务器将提供承载令牌来访问资源 .

现在,每个请求必须具有带有承载令牌的授权标头才能访问资源 .

在我的情况下SPA正在打开新选项卡以使用直接URL访问不同类型的报告(这是一项要求),因此在这种情况下我无法设置授权标头 .

有没有任何标准化的方法来处理这种情况?

我尝试了三个选项来处理这个问题 .

#Token in url ( not a good practice)

#Javascript Blob (some browsers doesn't support)

var config = {
            responseType:'arraybuffer',
            headers: {
                'Authorization': $window.sessionStorage.token
            }
        };

        $http.get(url, config).then(function onSuccess(response) {

            var contentType = response.headers("content-type");
            var blob = new Blob([response.data], { type: contentType });
            var fileURL = URL.createObjectURL(blob);
            window.open(fileURL);
        });

#Set cookies ( This is working but I'm looking for different solutions where only bearer token will be used for authorization)

这是我的报告控制器代码,用于获取报告

[Authorize(User="CanView")]
    public ContentResult GetReport(string type, int id)
    {
        return new ContentResult() { Content = someByte, ContentType = "application/pdf" };
    }