From 230eb5cbed7b738264842f3a17197a3a3583f42f Mon Sep 17 00:00:00 2001 From: willhhan Date: Fri, 27 Oct 2023 12:03:41 +0800 Subject: [PATCH] add csharp template code --- README.en.md | 36 ---------- README.md | 39 ---------- nonsdk-dotnet-sample.sln | 16 +++++ nonsdk-dotnet-sample/Program.cs | 123 ++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 75 deletions(-) delete mode 100644 README.en.md delete mode 100644 README.md create mode 100644 nonsdk-dotnet-sample.sln create mode 100644 nonsdk-dotnet-sample/Program.cs diff --git a/README.en.md b/README.en.md deleted file mode 100644 index 991d022..0000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# nonsdk-dotnet-sample - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md deleted file mode 100644 index 9bbe8d4..0000000 --- a/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# nonsdk-dotnet-sample - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/nonsdk-dotnet-sample.sln b/nonsdk-dotnet-sample.sln new file mode 100644 index 0000000..f013c7c --- /dev/null +++ b/nonsdk-dotnet-sample.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet_play", "dotnet_play\dotnet_play.csproj", "{97482D8B-5AD9-4882-BA9A-8ED989B01F82}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {97482D8B-5AD9-4882-BA9A-8ED989B01F82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97482D8B-5AD9-4882-BA9A-8ED989B01F82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97482D8B-5AD9-4882-BA9A-8ED989B01F82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97482D8B-5AD9-4882-BA9A-8ED989B01F82}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/nonsdk-dotnet-sample/Program.cs b/nonsdk-dotnet-sample/Program.cs new file mode 100644 index 0000000..239961b --- /dev/null +++ b/nonsdk-dotnet-sample/Program.cs @@ -0,0 +1,123 @@ +using System.Net.Http.Headers; +using System.Security.Cryptography; +using System.Text; + +internal class Program +{ + private static readonly HttpClient Client = new HttpClient(); + + static void Main(string[] args) + { + var secretId = "$secretId"; + var secretKey = "$secretKey"; + var service = "%(product_name)s"; + var version = "%(product_version)s"; + var action = "%(product_action)s"; + var body = $json; + var region = "$region"; + var resp = DoRequest(secretId, secretKey, service, version, action, body, region); + + Console.WriteLine(resp); + } + + static string DoRequest( + string secretId, string secretKey, + string service, string version, string action, + string body, string region + ) + { + var request = BuildRequest(secretId, secretKey, service, version, action, body, region); + var response = Client.Send(request); + return response.Content.ReadAsStringAsync().Result; + } + + static HttpRequestMessage BuildRequest( + string secretId, string secretKey, + string service, string version, string action, + string body, string region + ) + { + var host = "$url"; + var url = "https://" + host; + var contentType = "application/json; charset=utf-8"; + var timestamp = ((int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString(); + var auth = GetAuth(secretId, secretKey, host, contentType, timestamp, body); + var request = new HttpRequestMessage(); + request.Method = HttpMethod.Post; + request.Headers.Add("Host", host); + request.Headers.Add("X-TC-Timestamp", timestamp); + request.Headers.Add("X-TC-Version", version); + request.Headers.Add("X-TC-Action", action); + request.Headers.Add("X-TC-Region", region); + request.Headers.Add("X-TC-RequestClient", "SDK_NET_BAREBONE"); + request.Headers.TryAddWithoutValidation("Authorization", auth); + // request.Headers.Authorization = new AuthenticationHeaderValue(auth); + request.RequestUri = new Uri(url); + request.Content = new StringContent(body, MediaTypeWithQualityHeaderValue.Parse(contentType)); + Console.WriteLine(request); + return request; + } + + static string GetAuth( + string secretId, string secretKey, string host, string contentType, + string timestamp, string body + ) + { + var canonicalURI = "/"; + var canonicalHeaders = "content-type:" + contentType + "\nhost:" + host + "\n"; + var signedHeaders = "content-type;host"; + var hashedRequestPayload = Sha256Hex(body); + var canonicalRequest = "POST" + "\n" + + canonicalURI + "\n" + + "\n" + + canonicalHeaders + "\n" + + signedHeaders + "\n" + + hashedRequestPayload; + + var algorithm = "TC3-HMAC-SHA256"; + var date = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(int.Parse(timestamp)) + .ToString("yyyy-MM-dd"); + var service = host.Split('.')[0]; + var credentialScope = date + "/" + service + "/" + "tc3_request"; + var hashedCanonicalRequest = Sha256Hex(canonicalRequest); + var stringToSign = algorithm + "\n" + + timestamp + "\n" + + credentialScope + "\n" + + hashedCanonicalRequest; + + var tc3SecretKey = Encoding.UTF8.GetBytes("TC3" + secretKey); + var secretDate = HmacSha256(tc3SecretKey, Encoding.UTF8.GetBytes(date)); + var secretService = HmacSha256(secretDate, Encoding.UTF8.GetBytes(service)); + var secretSigning = HmacSha256(secretService, Encoding.UTF8.GetBytes("tc3_request")); + var signatureBytes = HmacSha256(secretSigning, Encoding.UTF8.GetBytes(stringToSign)); + var signature = BitConverter.ToString(signatureBytes).Replace("-", "").ToLower(); + + return algorithm + " " + + "Credential=" + secretId + "/" + credentialScope + ", " + + "SignedHeaders=" + signedHeaders + ", " + + "Signature=" + signature; + } + + public static string Sha256Hex(string s) + { + using (SHA256 algo = SHA256.Create()) + { + byte[] hashbytes = algo.ComputeHash(Encoding.UTF8.GetBytes(s)); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < hashbytes.Length; ++i) + { + builder.Append(hashbytes[i].ToString("x2")); + } + + return builder.ToString(); + } + } + + private static byte[] HmacSha256(byte[] key, byte[] msg) + { + using (HMACSHA256 mac = new HMACSHA256(key)) + { + return mac.ComputeHash(msg); + } + } +} \ No newline at end of file -- Gitee