首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在调用2 calls时使用C#进行身份验证

如何在调用2 calls时使用C#进行身份验证
EN

Stack Overflow用户
提问于 2022-07-21 11:35:53
回答 1查看 88关注 0票数 0

我正在尝试调用2。根据他们的文件,首先我需要认证。他们网站上的所有示例代码都是用PHP编写的。当我尝试使用同样的C#时,我从服务器获得了"Hash签名无法被验证“的消息。下面是从我的代码中截取的代码:

代码语言:javascript
复制
Encoding encoding = Encoding.UTF8;
string vendorCode = //My vendor code
string secretKey = //My secret key
byte[] secretBytes = encoding.GetBytes(secretKey);
date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string input = vendorCode.Length.ToString() + vendorCode + date.Length.ToString() + date;

using (HMACMD5 keyedHash = new HMACMD5(secretBytes))
{
    byte[] hashedBytes = keyedHash.ComputeHash(encoding.GetBytes(input));
    string hash = Convert.ToBase64String(hashedBytes);
    
    using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, apiUrl + 
requestString))
    {
        request.Headers.Add("accept", "application/json");
        string headerValue = "code=\"" + vendorCode + "\" date=\"" + date + "\" hash=\"" + hash + "\"";
        request.Headers.Add("X-Avangate-Authentication", headerValue);
        HttpResponseMessage httpResponse = await httpClient.SendAsync(request);
    }
}

我不知道我做错了什么。是我使用的哈希算法还是文本编码?我尝试了几种变体,但都没有成功。如果有人在这方面帮助我,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2022-11-08 10:09:53

下面的代码块适用于我。根据文档https://verifone.cloud/docs/2checkout/API-Integration/Webhooks/06Instant_Payment_Notification_%2528IPN%2529/IPN-code-samples#c__0023__00a0的需要稍微修改代码

代码语言:javascript
复制
string secretKey = "MYSECRETKEY";
string VendorCode = "MYVENDORCODE";
string requestDateTime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
string plaintext = VendorCode.Length + VendorCode + requestDateTime.Length + requestDateTime;

using(HMACMD5 hmac = new HMACMD5(Encoding.ASCII.GetBytes(secretKey)))
{
    byte[] hashBytes = hmac.ComputeHash(Encoding.ASCII.GetBytes(plaintext));        
    string signatureHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    
    using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.avangate.com/rest/6.0/") })                
    {
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
        httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Avangate-Authentication", $"code='{VendorCode}' date='{requestDateTime}' hash='{signatureHash}'");
        // Make your requests to desired Api
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73065452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档