P.Utility 26.2.28.1841

dotnet add package P.Utility --version 26.2.28.1841
                    
NuGet\Install-Package P.Utility -Version 26.2.28.1841
                    
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="P.Utility" Version="26.2.28.1841" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="P.Utility" Version="26.2.28.1841" />
                    
Directory.Packages.props
<PackageReference Include="P.Utility" />
                    
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 P.Utility --version 26.2.28.1841
                    
#r "nuget: P.Utility, 26.2.28.1841"
                    
#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 P.Utility@26.2.28.1841
                    
#: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=P.Utility&version=26.2.28.1841
                    
Install as a Cake Addin
#tool nuget:?package=P.Utility&version=26.2.28.1841
                    
Install as a Cake Tool

P.Utility

简介

P.Utility 是一个通用工具包,提供了各种实用工具类和方法,用于简化开发过程中的常见任务。本工具包专注于提供加密、解密、电子邮件发送、缓存管理、数据压缩、正则表达式验证和Active Directory操作等核心功能。

主要功能

电子邮件功能 (MailHelper)

  • 支持发送HTML格式的电子邮件
  • 支持添加附件
  • 支持设置邮件优先级和重要性
  • 支持抄送(CC)和密送(BCC)
  • 支持单独发送给每个收件人或合并发送
  • 支持异步发送方法

加密与安全 (MD5, DesHelper, CryptoHelper)

  • MD5哈希计算
  • DES加密与解密
  • AES加密与解密
  • RSA加密与解密
  • SHA1/SHA256/SHA512哈希计算
  • HMACSHA256消息认证码
  • 安全的密码哈希与验证
  • 支持自定义密钥

缓存管理 (CacheHelper)

  • 内存缓存的添加、获取和移除
  • 支持绝对过期时间和滑动过期时间
  • 自动清理过期缓存项
  • 支持异步缓存操作
  • 线程安全的缓存实现

数据压缩 (CompressionHelper)

  • 字符串压缩与解压缩
  • 字节数组压缩与解压缩
  • 文件压缩与解压缩
  • 文件夹压缩为ZIP文件
  • 支持异步压缩和解压缩操作

正则表达式 (RegexHelper)

  • 常用正则表达式模式预定义
  • 电子邮件、手机号码、身份证号码等格式验证
  • 正则表达式匹配和替换
  • 提取电子邮件、URL等信息
  • 密码强度检查
  • HTML标签清理

Active Directory操作 (ActiveDirectoryHelper)

  • 用户身份验证
  • 用户信息获取与搜索
  • 用户组管理
  • 用户密码重置
  • 账户锁定与解锁
  • 域控制器信息获取

使用方法

安装

使用 NuGet 包管理器安装:

Install-Package P.Utility

或者使用 .NET CLI:

dotnet add package P.Utility

示例代码

发送邮件
using P.Utility;
using MimeKit;
using MailKit;

// 创建发件人和收件人
var from = new MailboxAddress("发件人姓名", "sender@example.com");
var to = new List<MailboxAddress> { new MailboxAddress("收件人姓名", "recipient@example.com") };

// 发送邮件
MailHelper.SendMail(
    smtpServer: "smtp.example.com",
    account: "your-email@example.com",
    password: "your-password",
    from: from,
    to: to,
    subject: "测试邮件",
    body: "<h1>这是一封测试邮件</h1><p>Hello World!</p>",
    importance: MessageImportance.High
);

// 异步发送
await MailHelper.SendMailAsync(
    smtpServer: "smtp.example.com",
    account: "your-email@example.com",
    password: "your-password",
    from: from,
    to: to,
    subject: "测试邮件",
    body: "<h1>这是一封测试邮件</h1><p>Hello World!</p>"
);
MD5加密
using P.Utility;

string text = "Hello World";
string md5Hash = text.ToMD5(); // 返回MD5哈希值(大写)
DES加密与解密
using P.Utility;

string originalText = "需要加密的文本";
string key = "加密密钥";

// 加密
string encryptedText = DesHelper.EncryptDES(originalText, key);

// 解密
string decryptedText = DesHelper.DecryptDES(encryptedText, key);
加密解密助手
using P.Utility;

// AES加密与解密
string plainText = "需要加密的文本";
string key = "1234567890123456"; // 16字节密钥
string iv = "1234567890123456";  // 16字节IV(可选)

string encrypted = CryptoHelper.EncryptAES(plainText, key, iv);
string decrypted = CryptoHelper.DecryptAES(encrypted, key, iv);

// RSA加密与解密
var (publicKey, privateKey) = CryptoHelper.GenerateRSAKeys();
string rsaEncrypted = CryptoHelper.EncryptRSA(plainText, publicKey);
string rsaDecrypted = CryptoHelper.DecryptRSA(rsaEncrypted, privateKey);

// 哈希计算
string md5Hash = CryptoHelper.ComputeMD5("test");
string sha1Hash = CryptoHelper.ComputeSHA1("test");
string sha256Hash = CryptoHelper.ComputeSHA256("test");
string sha512Hash = CryptoHelper.ComputeSHA512("test");
string hmacHash = CryptoHelper.ComputeHMACSHA256("message", "key");

// 密码哈希与验证
string password = "P@ssw0rd";
string hashedPassword = CryptoHelper.HashPassword(password);
bool isValid = CryptoHelper.VerifyPassword(password, hashedPassword);
缓存管理
using P.Utility;

// 添加缓存项
CacheHelper.Set("key1", "value1");

// 添加带过期时间的缓存项
CacheHelper.Set("key2", "value2", DateTimeOffset.UtcNow.AddMinutes(10)); // 10分钟后过期
CacheHelper.Set("key3", "value3", null, TimeSpan.FromMinutes(5)); // 5分钟不访问后过期

// 获取缓存项
string value = CacheHelper.Get<string>("key1");
string valueWithDefault = CacheHelper.Get("nonExistingKey", "默认值"); // 如果键不存在,返回默认值

// 获取或添加缓存项
string cachedValue = CacheHelper.GetOrAdd("key4", () => 
{
    // 当缓存不存在时执行的代码,计算缓存值
    return "计算得到的值";
});

// 异步获取或添加缓存项
string asyncCachedValue = await CacheHelper.GetOrAddAsync("key5", async () => 
{
    // 异步计算缓存值
    await Task.Delay(100);
    return "异步计算得到的值";
});

// 移除缓存项
bool removed = CacheHelper.Remove("key1");

// 清空所有缓存
CacheHelper.Clear();

// 检查缓存项是否存在
bool exists = CacheHelper.Contains("key2");

// 获取缓存项数量
int count = CacheHelper.Count();
数据压缩
using P.Utility;
using System.Text;

// 字符串压缩与解压缩
string original = "这是一个需要压缩的长文本...";
string compressed = CompressionHelper.CompressString(original);
string decompressed = CompressionHelper.DecompressString(compressed);

// 字节数组压缩与解压缩
byte[] originalBytes = Encoding.UTF8.GetBytes(original);
byte[] compressedBytes = CompressionHelper.CompressBytes(originalBytes);
byte[] decompressedBytes = CompressionHelper.DecompressBytes(compressedBytes);

// 文件压缩与解压缩
bool compressResult = CompressionHelper.CompressFile("source.txt", "compressed.gz");
bool decompressResult = CompressionHelper.DecompressFile("compressed.gz", "decompressed.txt");

// 异步文件压缩与解压缩
bool asyncCompressResult = await CompressionHelper.CompressFileAsync("source.txt", "compressed.gz");
bool asyncDecompressResult = await CompressionHelper.DecompressFileAsync("compressed.gz", "decompressed.txt");

// 文件夹压缩与解压缩
bool folderCompressResult = CompressionHelper.CompressFolder("sourceFolder", "archive.zip");
bool folderDecompressResult = CompressionHelper.DecompressFolder("archive.zip", "extractedFolder");
正则表达式助手
using P.Utility;

// 验证电子邮件
bool isValidEmail = RegexHelper.IsValidEmail("user@example.com");

// 验证手机号码(中国大陆)
bool isValidPhone = RegexHelper.IsValidMobilePhone("13800138000");

// 验证身份证号码(中国大陆)
bool isValidIdCard = RegexHelper.IsValidIdCard("110101199003070953");

// 验证URL
bool isValidUrl = RegexHelper.IsValidUrl("https://example.com");

// 验证是否为中文字符
bool isChineseOnly = RegexHelper.IsChineseOnly("这是中文");

// 检查密码强度
int passwordStrength = RegexHelper.CheckPasswordStrength("P@ssw0rd"); // 返回0-4的强度值

// 提取电子邮件地址
List<string> emails = RegexHelper.ExtractEmails("联系我们:support@example.com 或 sales@example.com");

// 提取URL
List<string> urls = RegexHelper.ExtractUrls("访问我们的网站:https://example.com");

// 移除HTML标签
string plainText = RegexHelper.RemoveHtmlTags("<p>这是一个<strong>测试</strong>段落</p>");

// 正则表达式替换
string redactedText = RegexHelper.Replace(
    "我的电话是13800138000,请联系我",
    RegexHelper.MobilePhonePattern,
    "[电话已隐藏]"
);
Active Directory助手
using P.Utility;

// 验证用户凭据
string domain = "example.com";
bool isValid = ActiveDirectoryHelper.ValidateCredentials(domain, "username", "password");

// 获取用户信息
ADUserInfo userInfo = ActiveDirectoryHelper.GetUserInfo(domain, "username");
if (userInfo != null)
{
    Console.WriteLine($"用户名: {userInfo.Username}");
    Console.WriteLine($"显示名称: {userInfo.DisplayName}");
    Console.WriteLine($"电子邮件: {userInfo.Email}");
    Console.WriteLine($"是否启用: {userInfo.IsEnabled}");
    Console.WriteLine($"是否锁定: {userInfo.IsLocked}");
}

// 获取用户所属的组
List<string> groups = ActiveDirectoryHelper.GetUserGroups(domain, "username");
foreach (var group in groups)
{
    Console.WriteLine($"组: {group}");
}

// 检查用户是否属于指定组
bool isMember = ActiveDirectoryHelper.IsUserInGroup(domain, "username", "GroupName");

// 搜索用户
List<ADUserInfo> users = ActiveDirectoryHelper.SearchUsers(domain, "smith", 10);

// 重置用户密码
bool resetSuccess = ActiveDirectoryHelper.ResetUserPassword(domain, "username", "NewP@ssw0rd");

// 启用或禁用用户账户
bool enableSuccess = ActiveDirectoryHelper.EnableDisableUser(domain, "username", true);

// 解锁用户账户
bool unlockSuccess = ActiveDirectoryHelper.UnlockUserAccount(domain, "username");

// 获取当前登录用户信息
var (currentDomain, currentUsername) = ActiveDirectoryHelper.GetCurrentUser();

依赖项

  • .NET Framework 4.6.2+ 或 .NET Standard 2.0+
  • MailKit (用于邮件功能)
  • MimeKit (用于邮件功能)
  • System.DirectoryServices.AccountManagement (用于Active Directory功能)

版本历史

  • 25.6.15 - 添加加密解密助手、正则表达式助手和AD助手
    • 添加CryptoHelper类,提供AES/RSA加密和各种哈希功能
    • 添加RegexHelper类,提供常用正则表达式验证和匹配功能
    • 添加ActiveDirectoryHelper类,提供AD域用户和组管理功能
  • 25.6.13 - 添加缓存管理和数据压缩功能
    • 添加CacheHelper类,提供内存缓存功能
    • 添加CompressionHelper类,提供数据压缩功能
  • 25.6.12 - 初始版本,包含基本工具类
    • 添加邮件发送功能
    • 添加MD5哈希计算
    • 添加DES加密与解密
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.2.28.1841 131 2/28/2026
26.2.28.1233 125 2/28/2026
25.6.13.1315 356 6/13/2025
25.6.12.1839 371 6/12/2025
25.6.12.1835 373 6/12/2025
25.6.12.1656 371 6/12/2025
23.4.18.1431 295 4/18/2023
23.4.18.1425 279 4/18/2023
23.4.18.1410 290 4/18/2023
23.4.18.1402 290 4/18/2023
23.4.18.1341 268 4/18/2023
23.4.18.1121 289 4/18/2023
23.4.18.1054 289 4/18/2023

添加加密解密助手、正则表达式助手和AD助手