ASP.NET 中的令牌端点
本文介绍了在 ASP.NET 中实现的用于创建 JSON Web 令牌 (JWT) 的简单 令牌端点 示例。这些令牌由 CKEditor 云服务用于验证用户身份。
# 依赖项
这两个代码示例都使用了 System.IdentityModel.Tokens.Jwt 库。
如果您在 Visual Studio 中使用 包管理器控制台,则可以运行以下命令
Install-Package System.IdentityModel.Tokens.Jwt
# 示例
在创建令牌端点以与 协作 集成时,令牌负载应包含环境 ID 和用户数据。
# 实时协作功能
using System;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using System.Collections.Generic;
namespace CSTokenExample
{
class Program
{
static string createCSToken(string environmentId, string accessKey)
{
var securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(accessKey));
var signingCredentials = new SigningCredentials(securityKey, "HS256");
var header = new JwtHeader(signingCredentials);
var dateTimeOffset = new DateTimeOffset(DateTime.UtcNow);
var payload = new JwtPayload
{
{ "aud", environmentId },
{ "iat", dateTimeOffset.ToUnixTimeSeconds() },
{ "sub", "user-123" },
{ "user", new Dictionary<string, string> {
{ "email", "joe.doe@example.com" },
{ "name", "Joe Doe" }
} },
{ "auth", new Dictionary<string, object> {
{ "collaboration", new Dictionary<string, object> {
{ "*", new Dictionary<string, string> {
{ "role", "writer" }
} }
} }
} }
};
var securityToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
return handler.WriteToken(securityToken);
}
static void Main(string[] args)
{
string accessKey = "w1lnWEN63FPKxBNmxHN7WpfW2IoYVYca5moqIUKfWesL1Ykwv34iR5xwfWLy";
string environmentId = "LJRQ1bju55p6a47RwadH";
var tokenString = createCSToken(environmentId, accessKey);
// Here we are printing the token to the console. In a real usage scenario
// it should be returned in an HTTP response of the token endpoint.
Console.WriteLine(tokenString);
}
}
}
accessKey
和 environmentId
应替换为 CKEditor 生态系统客户仪表板 为 SaaS 提供的密钥或 管理面板 为本地应用程序提供的密钥。用户数据可以从会话或数据库中获取。
然后,您应该将令牌传递给令牌端点 HTTP 响应中的客户端。请勿忘记在发送令牌之前在您的应用程序中验证用户身份。如果用户未经身份验证,令牌端点应返回错误或重定向到登录页面。您还应确保令牌通过加密通道发送。
# 轻松图片、导出为 PDF 以及导入和导出为 Word
轻松图片和导出为 Word/PDF 功能的令牌端点不需要添加用户数据。因此,您可以跳过令牌负载中的 user
和 auth
属性。
# 导出为 PDF 以及导入和导出为 Word 本地
PDF 转换器和 DOCX 转换器本地的令牌不需要任何额外的声明,因此您可以使用空负载创建令牌。
在此实现中,accessKey
已被 SECRET_KEY
替换,这是一个在 导入和导出为 Word/导出为 PDF 本地实例安装期间设置的变量。
using System;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using System.Collections.Generic;
namespace CSTokenExample
{
class Program
{
static string createCSToken(string secretKey)
{
var securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
var signingCredentials = new SigningCredentials(securityKey, "HS256");
var header = new JwtHeader(signingCredentials);
var dateTimeOffset = new DateTimeOffset(DateTime.UtcNow);
var payload = new JwtPayload
{
{ "iat", dateTimeOffset.ToUnixTimeSeconds() }
};
var securityToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
return handler.WriteToken(securityToken);
}
static void Main(string[] args)
{
string secretKey = "w1lnWEN63FPKxBNmxHN7WpfW2IoYVYca5moqIUKfWesL1Ykwv34iR5xwfWLy";
var tokenString = createCSToken(secretKey);
// Here we are printing the token to the console. In a real usage scenario
// it should be returned in an HTTP response of the token endpoint.
Console.WriteLine(tokenString);
}
}
}
如果您创建自己的令牌端点,请勿忘记在发送令牌之前验证用户身份。
# 示例响应
结果应为纯文本格式。
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJMSlJRMWJqdTU1cDZhNDdSd2FkSCIsImlhdCI6MTY0OTIyOTQyMiwic3ViIjoidXNlci0xMjMiLCJ1c2VyIjp7ImVtYWlsIjoiam9lLmRvZUBleGFtcGxlLmNvbSIsIm5hbWUiOiJKb2UgRG9lIn0sImF1dGgiOnsiY29sbGFib3JhdGlvbiI6eyIqIjp7InJvbGUiOiJ3cml0ZXIifX19fQ._V-HXKKHU1E-saZxk4JTvgXdh1I7793nCEK91ubSZHY
# 调试
出于调试目的,可以使用 jwt.io。