//Client端调用 static void Main(string[] args) { string publicKey = "DpLMCOihcYI2i6DaMbso9Dzo1miy70G/3+UibTttjLSiJ3cco"; publicKey += "Kaen3Fecywdf7DrkcfkG3KjeMbZ6djBihD/4A=="; string privateKey = "W9cE42m+fmBXXvTpYDa2CXIme7DQmk3FcwX0zqR7fmj"; privateKey += "D6PHHliwdtRb5cOUaxpPyh+3C6Y5Z34uGb2DWD/Awiw=="; using (HttpClient client = new HttpClient()) { // Step 2-a int counter = 33; Uri uri = new Uri("http://localhost:54400/api/employees/12345"); client.DefaultRequestHeaders.Add("X-PSK", publicKey); client.DefaultRequestHeaders.Add("X-Counter", String.Format("{0}", counter)); // Step 2-b DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc); TimeSpan ts = DateTime.UtcNow - epochStart; string stamp = Convert.ToUInt64(ts.TotalSeconds).ToString(); client.DefaultRequestHeaders.Add("X-Stamp", stamp); string data = String.Format("{0}{1}{2}{3}{4}", publicKey, counter, stamp, uri.ToString(), "GET"); // Step 2-c byte[] signature = Encoding.UTF8.GetBytes(data); using (HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(privateKey))) { byte[] signatureBytes = hmac.ComputeHash(signature); client.DefaultRequestHeaders.Add("X-Signature", Convert.ToBase64String(signatureBytes)); } var httpMessage = client.GetAsync(uri).Result; if (httpMessage.IsSuccessStatusCode) Console.WriteLine(httpMessage.Content.ReadAsStringAsync().Result); } } //Server端认真 public class PskHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { string privateKey = "W9cE42m+fmBXXvTpYDa2CXIme7DQmk3FcwX0zqR7fmj"; privateKey += "D6PHHliwdtRb5cOUaxpPyh+3C6Y5Z34uGb2DWD/Awiw=="; var headers = request.Headers; if (headers.Contains("X-PSK") && headers.Contains("X-Counter") && headers.Contains("X-Stamp") && headers.Contains("X-Signature")) { string publicKey = headers.GetValues("X-PSK").First(); string counter = headers.GetValues("X-Counter").First(); ulong stamp = Convert.ToUInt64(headers.GetValues("X-Stamp").First()); string incomingSignature = headers.GetValues("X-Signature").First(); string data = String.Format("{0}{1}{2}{3}{4}", publicKey, counter, stamp, request.RequestUri.ToString(), request.Method.Method); byte[] signature = Encoding.UTF8.GetBytes(data); using (HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(privateKey))) { byte[] signatureBytes = hmac.ComputeHash(signature); if (incomingSignature.Equals(Convert.ToBase64String(signatureBytes), StringComparison.Ordinal)) { DateTime epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc); TimeSpan ts = DateTime.UtcNow - epochStart; if (Convert.ToUInt64(ts.TotalSeconds) - stamp <= 3) return await base.SendAsync(request, cancellationToken); } } } return request.CreateResponse(HttpStatusCode.Unauthorized); } }
非对称认证方式 可以用在 asp.net webapi 的安全机制里面
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Asp.Net Core 配置动态WebApi
.Net Core 配置动态WebApi
动态生成 Web 应用服务 -
Asp.net WEBAPI 简单的OAUTH认证
最近想买需要,公司使用了完全的前后端分离技术,后端使用ASP.NET WEBAPI实现,前简单:1. 创建授权过滤器
asp.net oauth ide .net 服务器 -
ASP.NET for WebApi
WebApi,听说过吧?呵呵。感觉比WebService,WCF要强。尤其是那个啥WCF,啥鬼东西,真难懂。真难搞。真难用。说比W
asp.net WebApi RESTful ide json -
ASP.NET基于角色的窗体安全认证机制
作者:Willmove && Heath Stewa
asp.net authorization authentication 数据库 credentials -
ASP.NET MVC的WebApi使用
不多说,我直接使用代码,过程如下1.创建一个继承ApiController的控制器 using
asp.net Web System 客户端程序 -
Asp.Net WebAPI 中Cookie 获取操作方式
1. api中的cookie使用详解: http://www.asp.net/web-api/overview/advanced/http-cookies
Asp.Net WebAPI Cookie asp.net