0

Der Code unten funktioniert nicht in WebMatrix ... (Nur der KVPs Teil nicht funktioniert)Wie Anfrage Abfragezeichenfolgeflag zu HMACSHA256 in 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("-", ""); 

} 

der gleiche Code funktioniert gut in Visual Studio Mvc app konvertieren. die einzige Änderung ist das "$" Zeichen in der KVPs Variable ... wenn $ in WebMatrix hinzugefügt wird, gibt es eine verschnörkelt rote Farbe

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}"); 

Antwort

1

Die $ neu in C# 6.0 unterstreicht. Es ist ein Operator für die String-Interpolation. Also, so etwas wie:

$"{kvp.Key}={kvp.Value}" 

Mittel buchstäblich ersetzen kvp.Key und kvp.Value mit den Werten dieser Bezeichner innerhalb des Strings. In kleineren Versionen von C# müssen Sie Folgendes tun:

String.Format("{0}={1}", kvp.Key, kvp.Value) 
Verwandte Themen