CommonClass.cs
2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Zircon.Base.Exceptions;
namespace PdfExportOpri.Common
{
public class CommonClass
{
public static Dictionary<string, object> DataRowToDictioniary(DataRow row)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
for (int i = 0; i < row.ItemArray.Length; i++)
{
dic.Add(row.Table.Columns[i].ColumnName, row[i]);
}
return dic;
}
public static string Md5(string content)
{
//MD5类是抽象类
MD5 md5 = MD5.Create();
//需要将字符串转成字节数组
byte[] buffer = Encoding.Default.GetBytes(content);
//加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] md5buffer = md5.ComputeHash(buffer);
string str = null;
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
foreach (byte b in md5buffer)
{
//得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
//但是在和对方测试过程中,发现我这边的MD5加密编码,经常出现少一位或几位的问题;
//后来分析发现是 字符串格式符的问题, X 表示大写, x 表示小写,
//X2和x2表示不省略首位为0的十六进制数字;
str += b.ToString("x2");
}
return str;
}
public static string GetRequest(HttpContext context, string key, object defaultValue, bool isRequired = false)
{
var endpoint = context.Features.Get<IEndpointFeature>()?.Endpoint; ;
var methodInfo = context.GetEndpoint()?.Metadata.GetMetadata<ControllerActionDescriptor>().MethodInfo;
string method = context.Request.Method.ToUpper();
if (method == "POST" || method == "PUT" || method == "PATCH")
{
if (context.Request.HasFormContentType && context.Request.Form.ContainsKey(key)) return context.Request.Form[key].ToString();
}
else
{
if (context.Request.Query.ContainsKey(key)) return context.Request.Query[key].ToString();
}
if (isRequired) throw new ClientException();
return defaultValue.ToString();
}
}
}