4 changed files with 234 additions and 23 deletions
@ -0,0 +1,47 @@ |
|||||
|
|
||||
|
using System; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
namespace Znyc.Dispatching.Core.Util |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 验证扩展类
|
||||
|
/// </summary>
|
||||
|
public static partial class Extensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 检查 Object 是否为 NULL
|
||||
|
/// </summary>
|
||||
|
/// <param name="value"></param>
|
||||
|
/// <returns></returns>
|
||||
|
public static bool IsEmpty(this object value) |
||||
|
{ |
||||
|
return value == null || string.IsNullOrEmpty(value.ToString()); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查 Object 是否为 NULL 或者 0
|
||||
|
/// </summary>
|
||||
|
/// <param name="value"></param>
|
||||
|
/// <returns></returns>
|
||||
|
public static bool IsNullOrZero(this object value) |
||||
|
{ |
||||
|
return value == null || value.ToString().Trim() == "0"; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查是否为 AJAX 请求
|
||||
|
/// </summary>
|
||||
|
/// <param name="request"></param>
|
||||
|
/// <returns></returns>
|
||||
|
public static bool IsAjaxRequest(this HttpRequest request) |
||||
|
{ |
||||
|
if (request == null) |
||||
|
throw new ArgumentNullException(nameof(request)); |
||||
|
|
||||
|
if (request.Headers != null) |
||||
|
return request.Headers["X-Requested-With"] == "XMLHttpRequest"; |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,93 @@ |
|||||
|
using Furion.JsonSerialization; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Security.Cryptography; |
||||
|
using System.Text; |
||||
|
using System.Text.Json.Serialization; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Znyc.Dispatching.Core.Util |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 小程序工具类
|
||||
|
/// </summary>
|
||||
|
public class MiniProgramUtil |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 解密数据获取小程序用户信息
|
||||
|
/// </summary>
|
||||
|
/// <param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
|
||||
|
/// <param name="session_key">auth.code2Session获取的session_key</param>
|
||||
|
/// <param name="iv">加密算法的初始向量</param>
|
||||
|
/// <returns>如遇异常返回 ""</returns>
|
||||
|
public static TelUserInfoModel AESDecrypt(string encryptedDataStr, string session_key, string iv) |
||||
|
{ |
||||
|
var rlt = BasicAESDecrypt(encryptedDataStr, session_key, iv); |
||||
|
return !rlt.IsEmpty() ? JSON.Deserialize<TelUserInfoModel>(rlt) : null; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 基础解析
|
||||
|
/// </summary>
|
||||
|
/// <param name="encryptedDataStr">包括敏感数据在内的完整用户信息的加密数据</param>
|
||||
|
/// <param name="session_key">auth.code2Session获取的session_key</param>
|
||||
|
/// <param name="iv">加密算法的初始向量</param>
|
||||
|
/// <returns></returns>
|
||||
|
private static string BasicAESDecrypt(string encryptedDataStr, string session_key, string iv) |
||||
|
{ |
||||
|
|
||||
|
var databyte = System.Text.Encoding.Default.GetBytes(encryptedDataStr); |
||||
|
// 16进制转换成byte
|
||||
|
var encryptedData = Convert.FromBase64String(encryptedDataStr.Replace(" ", "")); |
||||
|
var managed = new RijndaelManaged |
||||
|
{ |
||||
|
Key = Convert.FromBase64String(session_key), |
||||
|
IV = Convert.FromBase64String(iv), |
||||
|
Mode = CipherMode.CBC, |
||||
|
Padding = PaddingMode.PKCS7 |
||||
|
}; |
||||
|
var transform = managed.CreateDecryptor(); |
||||
|
var finalBlock = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); |
||||
|
var result = Encoding.Default.GetString(finalBlock); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static byte[] GetByteArray(string shex) |
||||
|
{ |
||||
|
string[] ssArray = shex.Split(' '); |
||||
|
List<byte> bytList = new List<byte>(); |
||||
|
foreach (var s in ssArray) |
||||
|
{ //将十六进制的字符串转换成数值
|
||||
|
bytList.Add(Convert.ToByte(s, 16)); |
||||
|
} //返回字节数组
|
||||
|
return bytList.ToArray(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 小程序登录解密后的用户信息Model
|
||||
|
/// </summary>
|
||||
|
public class TelUserInfoModel |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 手机号
|
||||
|
/// </summary>
|
||||
|
[JsonPropertyName("phoneNumber")] |
||||
|
public string PhoneNumber { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 纯手机号
|
||||
|
/// </summary>
|
||||
|
[JsonPropertyName("purePhoneNumber")] |
||||
|
public string PurePhoneNumber { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 区号
|
||||
|
/// </summary>
|
||||
|
[JsonPropertyName("countryCode")] |
||||
|
public string CountryCode { get; set; } |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue