惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Cloudflare Blog
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
D
Docker
Vercel News
Vercel News
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
爱范儿
爱范儿
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏

博客园 - 冠军

Keep alive 分析 SSRS Reporting Service 日志 [翻译] 感谢 Avalonia, .NET MAUI 即将来到 Linux 和浏览器端 多区域一致性:鱼与熊掌兼得! Microsoft Agent Framework (预览) 入门:让所有的开发者轻松创建 AI Agents 使用 AI app 模板扩展来创建基于订制数据进行聊天的 .NET AI 应用 真正的 .NET 单文件发布与构建 Microsoft.Extensions.AI 库 翻译:基于 OAuth2 在 Web 服务中使用 MailKit Microsoft.Extensions.VectorData.Abstractions 入门 基于本地 Llama 生成 .NET AI 矢量搜索应用 使用 PdfPig 处理 PDF 文档 使用 Any CPU构建 .NET 8 应用会支持 32 位吗? 使用 NATS CLI NATS: 使用 work-queue Stream NATS: Pull Consumers in JetStream .NET 单文件执行程序拆解器 SingleFileExtractor .NET Aspire Apps 集成测试 ASCII 与 Unicode 中的引号
OfficeRuntime.storage 适配 oidc-client.js 中的 WebStorage...
冠军 · 2025-07-30 · via 博客园 - 冠军

WebStorageStateStore 接口

WebStorageStateStore 将对底层存储的访问统一为如下 4 种操作:

  • Promise set(key, value)
  • Promise get(key)
  • Promise remove(key)
  • Promise getAllKeys()

需要注意的是返回类型都不是直接值,而使用 Promise 进行了封装。

对于基本的 Web 存储来说,它们都是一种 Storage,操作的返回值都是直接值,而不是 Promise。以 localStorage 为例,它提供了了:

  • None(undefined) setItem(keyName, keyValue)
  • string getItem(keyName)
  • None(undefined) removeItem(keyName)

在 WebStorageStateStore 中,这些返回值被封装为了 Promise。

export class WebStorageStateStore {
    constructor({prefix = "oidc.", store = Global.localStorage} = {}) {
        this._store = store;
        this._prefix = prefix;
    }

    set(key, value) {
        key = this._prefix + key;
        this._store.setItem(key, value);
        return Promise.resolve();
    }

    get(key) {
        key = this._prefix + key;
        let item = this._store.getItem(key);
        return Promise.resolve(item);
    }

    remove(key) {
        key = this._prefix + key;
        let item = this._store.getItem(key);
        this._store.removeItem(key);

        return Promise.resolve(item);
    }

    getAllKeys() {
        var keys = [];
        for (let index = 0; index < this._store.length; index++) {
            let key = this._store.key(index);

            if (key.indexOf(this._prefix) === 0) {
                keys.push(key.substr(this._prefix.length));
            }
        }

        return Promise.resolve(keys);
    }
}

源码:https://github.com/DuendeArchive/identity-model-oidc-client-js/blob/release/src/WebStorageStateStore.js

OfficeRuntime.storage

与标准的 Web Storage 不同,这个 Storage 的返回类型已经是 Promise 了。

  • Promise setItem(keyName, keyValue)
  • Promise<string | null> getItem(keyName)
  • Promise removeItem(keyName)

见:https://learn.microsoft.com/en-us/javascript/api/office-runtime/officeruntime.storage?view=common-js-preview

实现 OfficeRuntimeStorage 接口

因为 OfficeRuntime.Storage 的返回类型已经是 Promise 了,所以可以简化封装。下面是一个简单的示例。

export class OfficeRuntimeStorage {
    constructor({prefix = "oidc.", store = Global.localStorage} = {}) {
        this._store = store;
        this._prefix = prefix;
    }

    set(key, value) {
        key = this._prefix + key;
        this._store.setItem(key, value);
        return Promise.resolve();
    }

    get(key) {
        key = this._prefix + key;
        let item = this._store.getItem(key);
        return item;
    }

    remove(key) {
        key = this._prefix + key;
        let item = this._store.getItem(key);
        this._store.removeItem(key);

        return item;
    }

    getAllKeys() {
        let keys = this._store.getKeys();
        return keys;
    }
}

使用 OfficeRuntimeStorage

文档中的说明:https://github.com/DuendeArchive/identity-model-oidc-client-js/wiki

userStore: (default: session storage): Storage object used to persist User for currently authenticated user. E.g. userStore: new WebStorageStateStore({ store: window.localStorage })

使用 OfficeRuntimeStorage 的示例如下:

userStore: new OfficeRuntimeStorage({ store: OfficeRuntime.storage })

参考资料