Sparkdo.Security 1.0.4-preview.3

This is a prerelease version of Sparkdo.Security.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Sparkdo.Security --version 1.0.4-preview.3
                    
NuGet\Install-Package Sparkdo.Security -Version 1.0.4-preview.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sparkdo.Security" Version="1.0.4-preview.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparkdo.Security" Version="1.0.4-preview.3" />
                    
Directory.Packages.props
<PackageReference Include="Sparkdo.Security" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sparkdo.Security --version 1.0.4-preview.3
                    
#r "nuget: Sparkdo.Security, 1.0.4-preview.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sparkdo.Security@1.0.4-preview.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sparkdo.Security&version=1.0.4-preview.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sparkdo.Security&version=1.0.4-preview.3&prerelease
                    
Install as a Cake Tool

Sparkdo.Security

Sparkdo 安全模块,提供身份验证、授权、加密和安全日志等核心安全功能。

功能特性

  • 用户身份管理(ICurrentUser)
  • 客户端身份管理(ICurrentClient)
  • 声明式权限控制
  • 字符串加密服务
  • 安全日志记录
  • 模块化安全配置

安装

<PackageReference Include="Sparkdo.Security" Version="x.x.x" />

核心组件

用户和客户端管理

  • ICurrentUser: 访问当前认证用户信息
  • ICurrentClient: 访问当前客户端信息

加密服务

  • IStringEncryptionService: 字符串加密和解密

安全日志

  • ISecurityLogManager: 安全日志管理
  • ISecurityLogStore: 安全日志存储

模块配置

  • SparkdoSecurityModule: 自动注册安全服务

所有公共接口和类都添加了详细的 XML 文档注释,便于开发者理解和使用。

使用方法

基本身份信息访问

通过依赖注入获取当前用户信息:

public class MyService
{
    private readonly ICurrentUser _currentUser;
    
    public MyService(ICurrentUser currentUser)
    {
        _currentUser = currentUser;
    }
    
    public void DoSomething()
    {
        if (_currentUser.IsAuthenticated)
        {
            Console.WriteLine($"当前用户: {_currentUser.UserName}");
            Console.WriteLine($"用户ID: {_currentUser.Id}");
            Console.WriteLine($"用户角色: {string.Join(", ", _currentUser.Roles)}");
        }
    }
}

客户端身份信息访问

获取当前客户端信息:

public class MyService
{
    private readonly ICurrentClient _currentClient;
    
    public MyService(ICurrentClient currentClient)
    {
        _currentClient = currentClient;
    }
    
    public void DoSomething()
    {
        if (_currentClient.IsAuthenticated)
        {
            Console.WriteLine($"当前客户端ID: {_currentClient.Id}");
        }
    }
}

字符串加密

使用字符串加密服务:

public class MyService
{
    private readonly IStringEncryptionService _encryptionService;
    
    public MyService(IStringEncryptionService encryptionService)
    {
        _encryptionService = encryptionService;
    }
    
    public void EncryptData()
    {
        string plainText = "需要加密的数据";
        string encrypted = _encryptionService.Encrypt(plainText);
        string decrypted = _encryptionService.Decrypt(encrypted);
        
        Console.WriteLine($"原文: {plainText}");
        Console.WriteLine($"密文: {encrypted}");
        Console.WriteLine($"解密: {decrypted}");
    }
}

安全日志

记录安全相关操作:

public class MyService
{
    private readonly ISecurityLogManager _securityLogManager;
    
    public MyService(ISecurityLogManager securityLogManager)
    {
        _securityLogManager = securityLogManager;
    }
    
    public async Task DoSomethingAsync()
    {
        await _securityLogManager.SaveAsync(new SecurityLogInfo
        {
            Identity = "用户操作",
            Action = "数据修改",
            Message = "用户修改了重要数据",
            Parameters = "相关参数"
        });
    }
}

核心接口

ICurrentUser

当前用户信息接口,提供对当前认证用户信息的访问:

  • IsAuthenticated: 是否已通过身份验证
  • Id: 用户唯一标识
  • UserName: 用户名
  • Name: 用户姓名
  • Roles: 用户角色列表
  • FindClaim(): 查找特定声明
  • IsInRole(): 检查用户是否在特定角色中

ICurrentClient

当前客户端信息接口,提供对当前客户端信息的访问:

  • Id: 客户端唯一标识
  • IsAuthenticated: 客户端是否已认证

IStringEncryptionService

字符串加密服务接口:

  • Encrypt(): 加密字符串
  • Decrypt(): 解密字符串

ISecurityLogManager

安全日志管理器接口:

  • SaveAsync(): 保存安全日志

配置选项

SparkdoStringEncryptionOptions

字符串加密选项配置:

{
  "Sparkdo": {
    "StringEncryption": {
      "DefaultPassPhrase": "默认加密密钥",
      "DefaultSalt": "默认盐值",
      "KeySize": 256,
      "InitVectorBytes": "初始化向量"
    }
  }
}

扩展性

可以通过以下方式扩展安全功能:

  1. 实现自定义的 ICurrentUser 来提供特定的用户信息访问逻辑
  2. 实现自定义的 ICurrentClient 来提供特定的客户端信息访问逻辑
  3. 实现自定义的 IStringEncryptionService 来提供特定的加密算法
  4. 实现自定义的 ISecurityLogStore 来改变安全日志的存储方式

代码结构说明

本项目包含以下主要组件:

用户和客户端管理

  • ICurrentUserDefaultCurrentUser:用于访问当前认证用户的信息
  • ICurrentClientDefaultCurrentClient:用于访问当前客户端的信息
  • CurrentUserExtensions:提供对当前用户声明信息的扩展方法

安全声明处理

  • ICurrentPrincipalAccessor:用于访问当前安全主体
  • ISparkdoClaimsPrincipalFactory:用于创建声明主体
  • SparkdoClaimTypes:定义了系统中使用的声明类型常量
  • ClaimsIdentityExtensions:提供对声明身份的扩展方法

加密服务

  • IStringEncryptionServiceDefaultStringEncryptionService:提供字符串的加密和解密功能
  • SparkdoStringEncryptionOptions:配置加密服务的选项

安全日志

  • ISecurityLogManagerDefaultSecurityLogManager:用于管理安全日志
  • ISecurityLogStoreSimpleSecurityLogStore:用于存储安全日志
  • SecurityLogInfo:安全日志信息实体
  • SparkdoSecurityLogOptions:安全日志配置选项

模块配置

  • SparkdoSecurityModule:安全模块的配置类,用于注册所有安全相关服务

所有公共接口和类都添加了详细的 XML 文档注释,便于开发者理解和使用。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Sparkdo.Security:

Package Downloads
Sparkdo.Features

Sparkdo 功能系统库,提供功能定义、管理、检查和拦截功能

Sparkdo.Authorization

Sparkdo 授权决策运行时与 Invocation 执行点。

Sparkdo.AspNetCore.Authentication.JwtBearer

Sparkdo ASP.NET Core JWT Bearer 认证库,提供基于 JWT 的身份验证和授权功能实现

Sparkdo.AspNetCore.Authentication.OAuth

Sparkdo ASP.NET Core OAuth 认证库,提供 OAuth 认证功能的集成和配置

Sparkdo.Security.AspNetCore

Sparkdo Security 的 ASP.NET Core 主体投影适配。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0-preview.2 22 7/20/2026
2.0.0-preview.1 42 7/18/2026
1.0.4-preview.3 470 6/8/2026
1.0.4-preview.2 452 6/7/2026
1.0.4-preview.1 429 6/7/2026
1.0.3 1,039 6/6/2026
1.0.3-preview.1 454 5/17/2026
1.0.2 816 4/27/2026
1.0.2-preview.5 179 4/15/2026
1.0.2-preview.4 178 2/8/2026
1.0.2-preview.3 182 2/1/2026
1.0.2-preview.2 196 1/31/2026
1.0.2-preview.1 227 12/4/2025
1.0.1 2,829 11/27/2025
1.0.0 2,746 11/25/2025
1.0.0-preview.5 152 10/24/2025