From 87fda473d3c1780b3eaa617f60930f33230ffa83 Mon Sep 17 00:00:00 2001 From: xRain Date: Wed, 14 May 2025 06:26:10 +0800 Subject: [PATCH] udpate --- Communications/SimApiLoginItem.cs | 8 +++--- Helpers/SimApiAuth.cs | 4 +-- Helpers/SimApiCache.cs | 2 +- Helpers/SimApiJobWebAuth.cs | 2 +- Helpers/SimApiUtil.cs | 36 +++++++++++++++++++++++++- SimApi.csproj | 10 +++---- SimApiExtensions.cs | 43 ++++++++++++++++++++++++++----- 7 files changed, 83 insertions(+), 22 deletions(-) diff --git a/Communications/SimApiLoginItem.cs b/Communications/SimApiLoginItem.cs index 5693636..12ac03e 100644 --- a/Communications/SimApiLoginItem.cs +++ b/Communications/SimApiLoginItem.cs @@ -7,8 +7,8 @@ namespace SimApi.Communications; /// public class SimApiLoginItem { - public string? Id { get; set; } - public string[] Type { get; set; } = new[] { "user" }; - public Dictionary? Meta { get; set; } = null; - public object? Extra { get; set; } = null; + public string Id { get; set; } = null!; + public string[] Type { get; set; } = ["user"]; + public Dictionary Meta { get; set; } = new(); + public object? Extra { get; set; } }; \ No newline at end of file diff --git a/Helpers/SimApiAuth.cs b/Helpers/SimApiAuth.cs index bf3ba5f..6a8594d 100644 --- a/Helpers/SimApiAuth.cs +++ b/Helpers/SimApiAuth.cs @@ -1,6 +1,4 @@ -#nullable enable using System; -using System.Collections.Generic; using System.Text.Json; using Microsoft.Extensions.Caching.Distributed; using SimApi.Communications; @@ -33,7 +31,7 @@ public class SimApiAuth(IDistributedCache cache) public SimApiLoginItem? GetLogin(string token) { var login = cache.GetString(token); - return login != null ? JsonSerializer.Deserialize(login) : default; + return login != null ? JsonSerializer.Deserialize(login) : null; } /// diff --git a/Helpers/SimApiCache.cs b/Helpers/SimApiCache.cs index 4d4926f..7291a52 100644 --- a/Helpers/SimApiCache.cs +++ b/Helpers/SimApiCache.cs @@ -5,7 +5,7 @@ namespace SimApi.Helpers; public class SimApiCache(IDistributedCache cache) { - private const string Prefix = "SimApi:Cache"; + private const string Prefix = "SimApi:Cache:"; public void Set(string key, object value, DistributedCacheEntryOptions? options = null) { diff --git a/Helpers/SimApiJobWebAuth.cs b/Helpers/SimApiJobWebAuth.cs index 4b70d6c..f27519c 100644 --- a/Helpers/SimApiJobWebAuth.cs +++ b/Helpers/SimApiJobWebAuth.cs @@ -14,7 +14,7 @@ public class SimApiJobWebAuth(string user, string pass) : IDashboardAuthorizatio var authHeader = httpContext.Request.Headers["Authorization"].FirstOrDefault(); if (authHeader != null && authHeader.StartsWith("Basic ")) { - var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim(); + var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1].Trim(); var decodedUsernamePassword = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword)); var username = decodedUsernamePassword.Split(':', 2)[0]; var password = decodedUsernamePassword.Split(':', 2)[1]; diff --git a/Helpers/SimApiUtil.cs b/Helpers/SimApiUtil.cs index e1bdbae..cded92d 100644 --- a/Helpers/SimApiUtil.cs +++ b/Helpers/SimApiUtil.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; @@ -7,6 +8,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using System.Text.Unicode; +using System.Xml.Serialization; namespace SimApi.Helpers; @@ -22,6 +24,7 @@ public static class SimApiUtil /// public static JsonSerializerOptions JsonOption => new() { + ReferenceHandler = ReferenceHandler.Preserve, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull @@ -62,12 +65,43 @@ public static class SimApiUtil return stringBuilder.ToString(); } + /// + /// sha1加密字符串 + /// + /// 源字符串 + /// 加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位 + /// + public static string Sha1(string source, string mode = "x2") + { + var hash = SHA1.HashData(Encoding.UTF8.GetBytes(source)); + var sb = new StringBuilder(hash.Length * 2); + foreach (var b in hash) + { + sb.Append(b.ToString(mode)); + } + + return sb.ToString(); + } + + /// + /// 将XML字符串序列化为对象 + /// + /// + /// + /// + public static T XmlDeserialize(string source) + { + var xmlConvertor = new XmlSerializer(typeof(T)); + using var reader = new StringReader(source); + return (T)xmlConvertor.Deserialize(reader)!; + } + /// /// 将对象序列化成JSON (控制台输出中文不会被编码) /// /// /// - public static string Json(object obj) + public static string Json(object? obj) { return JsonSerializer.Serialize(obj, JsonOption); } diff --git a/SimApi.csproj b/SimApi.csproj index ca15b53..ac39b06 100644 --- a/SimApi.csproj +++ b/SimApi.csproj @@ -23,14 +23,14 @@ - + - - + + - - + + diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 4015ced..ed1e0db 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text.Json; +using System.Text.Json.Serialization; using Hangfire; using Hangfire.Console; using Hangfire.Redis.StackExchange; @@ -247,7 +248,11 @@ public static class SimApiExtensions { builder.AddControllers(opt => opt.Filters.Add()) .AddXmlSerializerFormatters() - .AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase); + .AddJsonOptions(opt => + { + opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + opt.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve; + }); } builder.AddSingleton(simApiOptions); @@ -328,22 +333,46 @@ public static class SimApiExtensions logger.LogInformation("开始配置SimApiAuth..."); builder.UseMiddleware(); builder.MapControllerRoute(name: "GetUserInfo", pattern: "/user/info", - defaults: new { controller = "SimApiCommon", action = "UserInfo" }); + defaults: new + { + controller = "SimApiCommon", + action = "UserInfo" + }); builder.MapControllerRoute(name: "CheckLogin", pattern: "/auth/check", - defaults: new { controller = "SimApiCommon", action = "CheckLogin" }); + defaults: new + { + controller = "SimApiCommon", + action = "CheckLogin" + }); builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout", - defaults: new { controller = "SimApiCommon", action = "Logout" }); + defaults: new + { + controller = "SimApiCommon", + action = "Logout" + }); if (options.EnableCoceSdk) { logger.LogInformation("开始配置CoceAppSdk...\nApi入口: {ApiUrl}\nAuth入口:{AuthUrl}n\nAppId: {AppId}", options.CoceSdkOptions.ApiEndpoint, options.CoceSdkOptions.AuthEndpoint, options.CoceSdkOptions.AppId); builder.MapControllerRoute(name: "LoginUseCoce", pattern: "/auth/login", - defaults: new { controller = "Coce", action = "Login" }); + defaults: new + { + controller = "Coce", + action = "Login" + }); builder.MapControllerRoute(name: "LoginUseCoce", pattern: "/user/groups", - defaults: new { controller = "Coce", action = "ListGroups" }); + defaults: new + { + controller = "Coce", + action = "ListGroups" + }); builder.MapControllerRoute(name: "LoginUseCoce", pattern: "/auth/config", - defaults: new { controller = "Coce", action = "GetConfig" }); + defaults: new + { + controller = "Coce", + action = "GetConfig" + }); } }