Last active
November 23, 2023 07:02
-
-
Save tresf/992d1411933b90981b77e7c9e38db7d8 to your computer and use it in GitHub Desktop.
C# PageMethods Signing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="js/qz-tray.js"></script> | |
<script> | |
qz.security.setSignatureAlgorithm("SHA512"); // Use "SHA1" for QZ Tray 2.0 and older | |
qz.security.setSignaturePromise(function (toSign) { | |
return function (resolve, reject) { | |
PageMethods.SignMessage(toSign, resolve, reject); | |
}; | |
}); | |
qz.security.setCertificatePromise(function (resolve, reject) { | |
return $.ajax({ url: "digital-certificate.txt", cache: false, dataType: "text" }).then(resolve, reject); | |
}); | |
// Unit test | |
qz.websocket.connect().then(qz.printers.getDefault) | |
.then(alert) | |
.catch(alert); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Web.Services; | |
private static X509KeyStorageFlags STORAGE_FLAGS = X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable; | |
[WebMethod] | |
public static string SignMessage(string request) | |
{ | |
//var WEBROOT_PATH = HttpContext.Current.Server.MapPath("/"); | |
//var CURRENT_PATH = HttpContext.Current.Server.MapPath("~"); | |
//var PARENT_PATH = System.IO.Directory.GetParent(WEBROOT_PATH).Parent.FullName; | |
var KEY = "/path/to/private-key.pfx"; | |
var PASS = ""; | |
try | |
{ | |
var cert = new X509Certificate2(KEY, PASS, STORAGE_FLAGS); | |
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey; | |
byte[] data = new ASCIIEncoding().GetBytes(request); | |
byte[] hash = new SHA1CryptoServiceProvider().ComputeHash(data); | |
return Convert.ToBase64String(csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA512"))); // Use "SHA1" for QZ Tray 2.0 and older | |
} | |
catch(Exception ex) | |
{ | |
if((STORAGE_FLAGS & X509KeyStorageFlags.MachineKeySet) == X509KeyStorageFlags.MachineKeySet) | |
{ | |
// IISExpress may fail with "Invalid provider type specified"; remove MachineKeySet flag, try again | |
STORAGE_FLAGS = STORAGE_FLAGS & ~X509KeyStorageFlags.MachineKeySet; | |
return SignMessage(request); | |
} | |
throw ex; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- settings.AutoRedirectMode = RedirectMode.Permanent; | |
+ settings.AutoRedirectMode = RedirectMode.Off; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- <asp:ScriptManager runat="server"> | |
+ <asp:ScriptManager runat="server" EnablePageMethods="true"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment