Le code ci-dessous ne fonctionne pas dans WebMatrix ... (Seule la partie KVPS ne fonctionne pas)Comment convertir demande queryString à HMACSHA256 en WebMatrix
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Web;
@using System.Configuration;
@using System.Text.RegularExpressions;
@using System.Collections.Specialized;
@using System.Security.Cryptography;
@using System.Text;
@using Newtonsoft.Json;
@{
Func<string, bool, string> replaceChars = (string s, bool isKey) =>
{
string output = (s.Replace("%", "%25").Replace("&", "%26")) ?? "";
if (isKey)
{
output = output.Replace("=", "%3D");
}
return output;
};
//This part is not working...
var kvps = Request.QueryString.Cast<string>()
.Select(s => new { Key = replaceChars(s, true), Value = replaceChars(Request.QueryString[s], false) })
.Where(kvp => kvp.Key != "signature" && kvp.Key != "hmac")
.OrderBy(kvp => kvp.Key)
.Select(kvp => "{kvp.Key}={kvp.Value}");
var hmacHasher = new HMACSHA256(Encoding.UTF8.GetBytes((string)AppState["secretKey"]));
var hash = hmacHasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("&", kvps)));
var calculatedSignature = BitConverter.ToString(hash).Replace("-", "");
}
le même code fonctionne bien en studio visuel app mvc. le seul changement est le « $ » signe dans la variable KVPS ... quand $ est ajouté à WebMatrix, il donne une couleur rouge squiggly souligner
var kvps = Request.QueryString.Cast<string>()
.Select(s => new { Key = replaceChars(s, true), Value = replaceChars(Request.QueryString[s], false) })
.Where(kvp => kvp.Key != "signature" && kvp.Key != "hmac")
.OrderBy(kvp => kvp.Key)
.Select(kvp => $"{kvp.Key}={kvp.Value}");