Logto 是一个为现代应用和 SaaS 产品设计的 Auth0 替代方案。它提供 Cloud 和 开源 服务,帮助你快速启动身份和管理 (IAM) 系统。享受认证 (Authentication)、授权 (Authorization) 和多租户管理 一体化。
我们建议从 Logto Cloud 上的免费开发租户开始。这可以让你轻松探索所有功能。
在本文中,我们将介绍使用 .NET Core (Blazor WebAssembly) 和 Logto 快速构建 Google Workspace enterprise SSO 登录体验(用户认证 (Authentication))的步骤。
先决条件
- 一个正在运行的 Logto 实例。查看 介绍页面 以开始。
- .NET Core (Blazor WebAssembly) 的基本知识。
- 一个可用的 Google Workspace enterprise SSO 账户。
Create an application in Logto
Logto 基于 OpenID Connect (OIDC) 认证 (Authentication) 和 OAuth 2.0 授权 (Authorization)。它支持跨多个应用程序的联合身份管理,通常称为单点登录 (SSO)。
要创建你的 单页应用 应用程序,只需按照以下步骤操作:
- 打开 Logto Console。在“开始使用”部分,点击“查看全部”链接以打开应用程序框架列表。或者,你可以导航到 Logto Console > Applications,然后点击“创建应用程序”按钮。
- 在打开的模态窗口中,点击“单页应用”部分,或使用左侧的快速过滤复选框过滤所有可用的“单页应用”框架。点击 ".NET Core (Blazor WebAssembly)" 框架卡片以开始创建你的应用程序。
- 输入应用程序名称,例如“Bookstore”,然后点击“创建应用程序”。
🎉 太棒了!你刚刚在 Logto 中创建了你的第一个应用程序。你将看到一个祝贺页面,其中包含详细的集成指南。按照指南查看你的应用程序中的体验将会是什么样的。
Integrate .NET Core (Blazor WebAssembly) SDK
- 以下演示基于 .NET Core 8.0 和 Blorc.OpenIdConnect。
- .NET Core 示例项目可在 GitHub 仓库 中找到。
安装
将 NuGet 包添加到你的项目中:
dotnet add package Blorc.OpenIdConnect
添加脚本引用
在 index.html
文件中包含 Blorc.Core/injector.js
:
<head>
<!-- ... -->
<script src="_content/Blorc.Core/injector.js"></script>
<!-- ... -->
</head>
注册服务
将以下代码添加到 Program.cs
文件中:
using Blorc.OpenIdConnect;
using Blorc.Services;
builder.Services.AddBlorcCore();
builder.Services.AddAuthorizationCore();
builder.Services.AddBlorcOpenIdConnect(
options =>
{
builder.Configuration.Bind("IdentityServer", options);
});
var webAssemblyHost = builder.Build();
await webAssemblyHost
.ConfigureDocumentAsync(async documentService =>
{
await documentService.InjectBlorcCoreJsAsync();
await documentService.InjectOpenIdConnectAsync();
});
await webAssemblyHost.RunAsync();
无需使用 Microsoft.AspNetCore.Components.WebAssembly.Authentication
包。Blorc.OpenIdConnect
包将负责认证过程。
配置重定向 URI
在我们深入细节之前,这里是终端用户体验的快速概述。登录过程可以简化如下:
- 你的应用调用登录方法。
- 用户被重定向到 Logto 登录页面。对于原生应用,将打开系统浏览器。
- 用户登录并被重定向回你的应用(配置为重定向 URI)。
关于基于重定向的登录
- 此认证 (Authentication) 过程遵循 OpenID Connect (OIDC) 协议,Logto 强制执行严格的安全措施以保护用户登录。
- 如果你有多个应用程序,可以使用相同的身份提供商 (IdP)(日志 (Logto))。一旦用户登录到一个应用程序,当用户访问另一个应用程序时,Logto 将自动完成登录过程。
要了解有关基于重定向的登录的原理和好处的更多信息,请参阅 Logto 登录体验解释。
在以下代码片段中,我们假设你的应用程序运行在 http://localhost:3000/
。
配置重定向 URI
切换到 Logto Console 的应用详情页面。添加一个重定向 URI http://localhost:3000/callback
。
就像登录一样,用户应该被重定向到 Logto 以注销共享会话。完成后,最好将用户重定向回你的网站。例如,添加 http://localhost:3000/
作为注销后重定向 URI 部分。
然后点击“保存”以保存更改。
配置应用程序
将以下代码添加到 appsettings.json
文件中:
{
// ...
IdentityServer: {
Authority: 'https://<your-logto-endpoint>/oidc',
ClientId: '<your-logto-app-id>',
PostLogoutRedirectUri: 'http://localhost:3000/',
RedirectUri: 'http://localhost:3000/callback',
ResponseType: 'code',
Scope: 'openid profile', // 如有需要可添加更多 scopes
},
}
记得将 RedirectUri
和 PostLogoutRedirectUri
添加到 Logto 应用程序设置中允许的重定向 URI 列表中。它们都是你的 WASM 应用程序的 URL。
添加 AuthorizeView
组件
在需要认证的 Razor 页面中,添加 AuthorizeView
组件。假设是在 Home.razor
页面:
@using Microsoft.AspNetCore.Components.Authorization
@page "/"
<AuthorizeView>
<Authorized>
@* 已登录视图 *@
<button @onclick="OnLogoutButtonClickAsync">
Sign out
</button>
</Authorized>
<NotAuthorized>
@* 未认证视图 *@
<button @onclick="OnLoginButtonClickAsync">
Sign in
</button>
</NotAuthorized>
</AuthorizeView>
设置认证
在 Home.razor.cs
文件中(如果不存在则创建),添加以下代码:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Blorc.OpenIdConnect;
using Microsoft.AspNetCore.Components.Authorization;
[Authorize]
public partial class Home : ComponentBase
{
[Inject]
public required IUserManager UserManager { get; set; }
public User<Profile>? User { get; set; }
[CascadingParameter]
protected Task<AuthenticationState>? AuthenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
User = await UserManager.GetUserAsync<User<Profile>>(AuthenticationStateTask!);
}
private async Task OnLoginButtonClickAsync(MouseEventArgs obj)
{
await UserManager.SignInRedirectAsync();
}
private async Task OnLogoutButtonClickAsync(MouseEventArgs obj)
{
await UserManager.SignOutRedirectAsync();
}
}
一旦用户通过认证,User
属性将被填充用户信息。
检查点:测试你的应用程序
现在,你可以测试你的应用程序:
- 运行你的应用程序,你将看到登录按钮。
- 点击登录按钮,SDK 将初始化登录过程并将你重定向到 Logto 登录页面。
- 登录后,你将被重定向回你的应用程序,并看到登出按钮。
- 点击登出按钮以清除令牌存储并登出。
Add Google Workspace enterprise SSO connector
To simplify access management and gain enterprise-level safeguards for your big clients, connect with .NET Core (Blazor WebAssembly) as a federated identity provider. The Logto enterprise SSO connector helps you establish this connection in minutes by allowing several parameter inputs.
To add an enterprise SSO connector, simply follow these steps:
- Navigate to Logto console > Enterprise SSO.
- Click "Add enterprise connector" button and choose your SSO provider type. Choose from prebuilt connectors for Microsoft Entra ID (Azure AD), Google Workspace, and Okta, or create a custom SSO connection using the standard OpenID Connect (OIDC) or SAML protocol.
- Provide a unique name (e.g., SSO sign-in for Acme Company).
- Configure the connection with your IdP in the "Connection" tab. Check the guides above for each connector types.
- Customize the SSO experience and enterprise’s email domain in the "Experience" tab. Users sign in with the SSO-enabled email domain will be redirected to SSO authentication.
- Save changes.
Set up Google Cloud Platform
步骤 1:在 Google Cloud Platform 上创建一个新项目
在你可以使用 Google Workspace 作为认证 (Authentication) 提供商之前,你必须在 Google API Console 中设置一个项目以获取 OAuth 2.0 凭证。如果你已经有一个项目,可以跳过这一步。否则,请在你的 Google 组织下创建一个新项目。
步骤 2:为你的应用程序配置用户授权页面
为了创建新的 OIDC 凭证,你需要为你的应用程序配置用户授权页面 (Consent screen)。
- 导航到 OAuth 用户授权页面 (Consent screen) 页面并选择
Internal
用户类型。这将使 OAuth 应用程序仅对你的组织内的用户可用。
- 按照页面上的说明填写
用户授权页面 (Consent Screen)
设置。你需要提供以下最少信息:
- 应用程序名称:你的应用程序的名称。它将在用户授权页面上显示。
- 支持邮箱:你的应用程序的支持邮箱。它将在用户授权页面上显示。
- 为你的应用程序设置
权限 (Scopes)
。为了正确从身份提供商 (IdP) 检索用户的身份信息和电子邮件地址,Logto SSO 连接器需要从 IdP 授予以下权限 (Scopes):
- openid:此权限是 OIDC 认证 (Authentication) 所需的。它用于检索 ID 令牌 (ID token) 并访问 IdP 的 userInfo 端点。
- profile:此权限用于访问用户的基本个人信息。
- email:此权限用于访问用户的电子邮件地址。
点击 Save
按钮以保存用户授权页面设置。
步骤 3:创建一个新的 OAuth 凭证
导航到 Credentials 页面并点击 Create Credentials
按钮。从下拉菜单中选择 OAuth client ID
选项,为你的应用程序创建一个新的 OAuth 凭证。
继续设置 OAuth 凭证,填写以下信息:
- 选择
Web application
作为应用程序类型。 - 填写你的客户端应用程序的
Name
,例如Logto SSO Connector
。这将帮助你在未来识别这些凭证。 - 在
Authorized redirect URIs
中填写 Logto 回调 URI。这是 Google 在成功认证 (Authentication) 后将用户的浏览器重定向到的 URI。当用户成功通过 IdP 认证 (Authentication) 后,IdP 会将用户的浏览器重定向回这个指定的 URI,并附带一个授权 (Authorization) 代码。Logto 将根据从此 URI 接收到的授权 (Authorization) 代码完成认证 (Authentication) 过程。 - 在
Authorized JavaScript origins
中填写 Logto 回调 URI 的来源。这确保只有你的 Logto 应用程序可以向 Google OAuth 服务器发送请求。 - 点击
Create
按钮以创建 OAuth 凭证。
步骤 4:使用客户端凭证设置 Logto 连接器
成功创建 OAuth 凭证后,你将收到一个包含客户端 ID 和客户端密钥的提示模式窗口。
复制 Client ID
和 Client secret
,并填写到 Logto 的 SSO 连接器 Connection
选项卡中的相应字段。
现在你已经在 Logto 上成功配置了一个 Google Workspace SSO 连接器。
步骤 5:附加权限 (Scopes)(可选)
使用 Scope
字段向你的 OAuth 请求添加额外的权限 (Scopes)。这将允许你从 Google OAuth 服务器请求更多信息。请参考 Google OAuth Scopes 文档以获取更多信息。
无论自定义权限 (Scope) 设置如何,Logto 总是会向身份提供商 (IdP) 发送 openid
、profile
和 email
权限 (Scopes)。这是为了确保 Logto 能够正确检索用户的身份信息和电子邮件地址。
步骤 6:设置电子邮件域并启用 SSO 连接器
在 Logto 的连接器 SSO 体验
标签中提供你组织的 电子邮件域
。这将启用 SSO 连接器作为这些用户的认证 (Authentication) 方法。
具有指定域的电子邮件地址的用户将被重定向,以使用你的 SSO 连接器作为他们唯一的认证 (Authentication) 方法。
有关 Google Workspace SSO 连接器的更多信息,请查看 Google OpenID Connector。
Save your configuration
仔细检查你是否在 Logto 连接器配置区域填写了必要的值。点击“保存并完成”(或“保存更改”),Google Workspace enterprise SSO 连接器现在应该可用了。
Enable Google Workspace enterprise SSO connector in Sign-in Experience
You don’t need to configure enterprise connectors individually, Logto simplifies SSO integration into your applications with just one click.
- Navigate to: Console > Sign-in experience > Sign-up and sign-in.
- Enable the "Enterprise SSO" toggle.
- Save changes.
Once enabled, a "Single Sign-On" button will appear on your sign-in page. Enterprise users with SSO-enabled email domains can access your services using their enterprise identity providers (IdPs).
To learn more about the SSO user experience, including SP-initiated SSO and IdP-initiated SSO, refer to User flows: Enterprise SSO.
Testing and Validation
返回到你的 .NET Core (Blazor WebAssembly) 应用。你现在应该可以使用 Google Workspace enterprise SSO 登录了。享受吧!
Further readings
终端用户流程:Logto 提供开箱即用的认证 (Authentication) 流程,包括多因素认证 (MFA) 和企业单点登录 (SSO),以及强大的 API,用于灵活实现账户设置、安全验证和多租户体验。
授权 (Authorization):授权 (Authorization) 定义了用户在被认证 (Authentication) 后可以执行的操作或访问的资源。探索如何保护你的 API 以用于原生和单页应用程序,并实现基于角色的访问控制 (RBAC)。
组织 (Organizations):在多租户 SaaS 和 B2B 应用中特别有效,组织功能支持租户创建、成员管理、组织级 RBAC 和即时供应。
客户 IAM 系列:我们关于客户(或消费者)身份和访问管理的系列博客文章,从 101 到高级主题及更深入的内容。