如何使用具有基本身份验证(NTLM、协商)的 Service Worker [英] How to use a Service Worker With BASIC Authentication (NTLM, Negotiate)

查看:69
本文介绍了如何使用具有基本身份验证(NTLM、协商)的 Service Worker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在 IIS 托管的网站中使用服务工作者来缓存网站的一些静态内容.该站点是使用 Windows 身份验证的内部应用程序.我已经能够在没有太多麻烦的情况下注册和运行 service worker,但是一旦我打开缓存并开始向缓存添加文件,promise 就会因授权失败而失败.返回的 HTTP 结果是 401 Unauthorised.这是前几个请求的通常响应,直到浏览器和服务器能够协商授权.

I have been trying to use a service worker within a IIS hosted web site that caches some of the static content of the site. The site is an internal application that uses Windows Authentication. I have been able to register and run a service worker without too much hassle, but as soon as I open the caches and start adding files to the cache, the promise fails with an authorisation failure. the returned HTTP result is 401 Unauthorised. This is the usual response for the first few requests until the browser and the server are able to negotiate the authorisation.

我将很快发布一些有助于解释的代码.

I will post some code soon that should help with the explanation.

编辑

var staticCacheName = 'app-static-v1';
console.log("I AM ALIVE");
this.addEventListener('install', function (event) {
    console.log("AND I INSTALLED!!!!");
    var urlsToCache = [
        //...many js files to cache
        '/scripts/numeral.min.js?version=2.2.0',
        '/scripts/require.js',
        '/scripts/text.js?version=2.2.0',
        '/scripts/toastr.min.js?version=2.2.0',
    ];


    event.waitUntil(
        caches.open(staticCacheName).then(function (cache) {
            cache.addAll(urlsToCache);
        }).catch(function (error) {
            console.log(error);
        })
    );
});

推荐答案

这只是一个猜测,因为缺少代码,但如果你正在做类似的事情:

This is just a guess, given the lack of code, but if you're doing something like:

caches.open('my-cache').then(cache => {
  return cache.add('page1.html'); // Or caches.addAll(['page1.html, page2.html']);
});

您正在利用隐式 Request 对象创建(参见第 6.4.4.4.1 节)当您将字符串传递给 cache.add()/cache.addAll().创建的 Request 对象使用默认的凭据模式,即 'omit'.

you're taking advantage of the implicit Request object creation (see section 6.4.4.4.1) that happens when you pass in a string to cache.add()/cache.addAll(). The Request object that's created uses the default credentials mode, which is 'omit'.

您可以做的是明确构造一个 Request 对象,其中包含您喜欢的凭据模式,在您的情况下可能是 'same-origin':

What you can do instead is explicitly construct a Request object containing the credentials mode you'd prefer, which in your case would likely be 'same-origin':

caches.open('my-cache').then(cache => {
  return cache.add(new Request('page1.html', {credentials: 'same-origin'}));
});

如果您有一堆 URL 将数组传递给 cache.addAll(),您可以将它们 .map() 传递给相应的 <代码>请求s:

If you had a bunch of URLs that you were passing an array to cache.addAll(), you can .map() them to a corresponding array of Requests:

var urls = ['page1.html', 'page2.html'];
caches.open('my-cache').then(cache => {
  return cache.addAll(urls.map(url => new Request(url, {credentials: 'same-origin'})));
});

这篇关于如何使用具有基本身份验证(NTLM、协商)的 Service Worker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆