开发工具提供程序使用 msal 节点登录用户并获取令牌,以便与 Microsoft Graph应用程序一同使用。
若要了解有关身份验证提供程序的信息,请参阅 提供程序。
入门
安装程序包
npm install @microsoft/mgt-element @microsoft/mgt-electron-provider
你需要在呈现器进程中初始化 (前端) ,在前端) 主进程中初始化 (Authenticator) 。
在呈现器进程中 (Renderer.ts)
在主进程) 中,将使用一个功能控制程序 (与一个操作者通信,以请求访问令牌,并接收组件正常工作所需的有关登录状态的信息。
import {Providers} from '@microsoft/mgt-element';
import {ElectronProvider} from '@microsoft/mgt-electron-provider/dist/Provider';
import '@microsoft/mgt-components';
// initialize the auth provider globally
Providers.globalProvider = new ElectronProvider();
在 main.ts (主进程中初始化一)
为 MSAL 身份验证设置配置变量、获取访问令牌以及与一起与一起通信时,一直由一位用户负责使用一个配置变量。 在主进程中初始化一个为一个,然后设置 Client-id 等配置变量。
import { ElectronAuthenticator, MsalElectronConfig } from '@microsoft/mgt-electron-provider/dist/Authenticator';
let config: MsalElectronConfig = {
clientId: '<your_client_id>',
authority: '<your_authority_url>', //optional
mainWindow: mainWindow,
scopes: ['user.read'] //We recommend pre-consenting all the required scopes on the Azure portal
};
ElectronAuthenticator.initialize(config);
| 属性 | 说明 |
|---|---|
| clientId | 字符串客户端 ID (创建应用/客户端 ID) 。 必需项。 |
| scopes | 用户必须同意登录的范围的逗号分隔字符串。 推荐。 |
| authority | 颁发机构字符串 - 默认为公共颁发机构。 对于单租户应用,请使用租户 ID 或租户名称。 例如,https://login.microsoftonline.com/[your-tenant-name].onmicrosoft.com 或 https://login.microsoftonline.com/[your-tenant-id]。 可选。 |
| mainWindow | 需要身份验证的主 BrowserWindow 的实例。 |
| cachePlugin | 要用于永久存储令牌的缓存插件。 请参阅 Microsoft Authentication Extensions for Node。 可选。 |
注意: 目前,提供程序不支持增量支持。 作为最佳实践,请确保同意组件需要的所有范围。
创建应用/客户端 ID
在客户端中添加新Azure Active Directory以获取客户端 ID
若要在应用程序中创建Azure Active Directory,请添加新的应用程序注册,然后配置应用名称和重定向 URI。
若要在应用中创建Azure Active Directory:
- 转到“Azure 门户”。
- 从菜单中,选择"Azure Active Directory "。
- 从"Azure Active Directory"菜单中,选择"应用注册 "。
- 从顶部菜单中,选择"新建 注册" 按钮。
- 输入应用的名称;例如,
My Electron-App。 - 对于受支持的帐户类型 类型,选择任何组织目录中的帐户 (任何 Azure AD 目录 - 多租户 ) 和个人 Microsoft 帐户 (例如 Skype、Xbox)。
- In the Redirect URI field, in the dropdown, select Public client/native (mobile & desktop), and in the URL field, enter
msal://redirect. - 通过选择"注册" 按钮确认 更改。
后续步骤
历史资料。此文对应 2023-02-03 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者(原始作者元数据 sebastienlevert)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。
