Created
September 19, 2023 20:32
-
-
Save thuutien/b5c74824fa74fc12177d2b86ae5e5297 to your computer and use it in GitHub Desktop.
Authenticate NetSuite functions
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
private string ComputeNonce() | |
{ | |
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | |
byte[] data = new byte[20]; | |
rng.GetBytes(data); | |
int value = Math.Abs(BitConverter.ToInt32(data, 0)); | |
return value.ToString(); | |
} | |
private long ComputeTimestamp() | |
{ | |
return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds); | |
} | |
private TokenPassportSignature ComputeSignature(string compId, string consumerKey, string consumerSecret, | |
string tokenId, string tokenSecret, string nonce, long timestamp) | |
{ | |
string baseString = compId + "&" + consumerKey + "&" + tokenId + "&" + nonce + "&" + timestamp; | |
string key = consumerSecret + "&" + tokenSecret; | |
string signature = ""; | |
var encoding = new System.Text.ASCIIEncoding(); | |
byte[] keyBytes = encoding.GetBytes(key); | |
byte[] baseStringBytes = encoding.GetBytes(baseString); | |
using (var hmacSha256 = new HMACSHA256(keyBytes)) | |
{ | |
byte[] hashBaseString = hmacSha256.ComputeHash(baseStringBytes); | |
signature = Convert.ToBase64String(hashBaseString); | |
} | |
TokenPassportSignature sign = new TokenPassportSignature(); | |
sign.algorithm = "HMAC_SHA256"; | |
sign.Value = signature; | |
return sign; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment