Hello Jason,
Here is the our code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using OAuth;
using System.Text;
using WSESimpleTCPDLL;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
/// <summary>
/// Summary description for oBasePage
/// </summary>
public class oBasePage :
System.Web.UI.Page
{
HttpContext htp;
public oBasePage(HttpContext _htp)
{
htp = _htp;
}
public bool isvalidrequest()
{
string AppId =
System.Configuration.ConfigurationManager.AppSettings["oAppId"].ToString();
if (htp.Request.QueryString["opensocial_app_id"].ToString() !=
AppId)
{
//
htp.Response.Write(htp.Request.QueryString["opensocial_app_id"].ToString());
//Response.End()
return false;
}
X509Certificate Cert =
X509Certificate.CreateFromCertFile(htp.Request.PhysicalApplicationPath
+ "/bin/pub.1199819524.-1556113204990931254.cer");
//Getting Post variables
string http_params;
NameValueCollection _p = htp.Request.Form;
Hashtable _hp = new Hashtable();
foreach (string k in _p.Keys)
{
if (k != null)
{
_hp[k] = _p[k];
}
}
SortedList _ps = new SortedList((IDictionary)_hp);
string[] pairs1 = new string[_ps.Keys.Count];
int l = 0;
foreach (string name in _ps.Keys)
{
if (name == "__display_name")
{
pairs1[l++] = name + "=" +
Uri.EscapeDataString(_ps[name].ToString());
//htp.Response.Write("" +
Uri.EscapeDataString(_ps[name].ToString()));
}
else
{
pairs1[l++] = name + "=" +
Uri.EscapeDataString(_ps[name].ToString());
}
}
http_params = (String.Join("&", pairs1));
if (_ps.Keys.Count != 0)
{
http_params = "&" + http_params;
}
RSACryptoServiceProvider Provider =
CertUtil.GetCertPublicKey(Cert);
OAuth.OAuthBase ba = new OAuthBase();
string signature =
(htp.Request.QueryString["oauth_signature"]);
string baseString = ba.GenerateSignatureBase(htp.Request.Url,
htp.Request.QueryString["oauth_consumer_key"], "",
htp.Request.QueryString["oauth_token"], "", htp.Request.HttpMethod,
htp.Request.QueryString["oauth_timestamp"],
htp.Request.QueryString["oauth_nonce"], "RSA-SHA1", http_params);
byte[] sign = Convert.FromBase64String(signature);
byte[] bstring = Encoding.UTF8.GetBytes(baseString);
return (Provider.VerifyData(bstring, "SHA1", sign));
}
}
/// <summary>
/// Summary description for CertUtil.
/// </summary>
public sealed class CertUtil
{
const uint CERT_SYSTEM_STORE_CURRENT_USER = 0x00010000;
const uint CERT_STORE_READONLY_FLAG = 0x00008000;
const uint CERT_STORE_OPEN_EXISTING_FLAG = 0x00004000;
const uint CERT_FIND_SUBJECT_STR = 0x00080007;
const uint X509_ASN_ENCODING = 0x00000001;
const uint PKCS_7_ASN_ENCODING = 0x00010000;
const uint RSA_CSP_PUBLICKEYBLOB = 19;
const int AT_KEYEXCHANGE = 1; //keyspec values
const int AT_SIGNATURE = 2;
static uint ENCODING_TYPE = PKCS_7_ASN_ENCODING |
X509_ASN_ENCODING;
private CertUtil()
{
}
public static RSACryptoServiceProvider
GetCertPublicKey(X509Certificate cert)
{
byte[] publickeyblob;
byte[] encodedpubkey = cert.GetPublicKey(); //asn.1 encoded
public key
uint blobbytes = 0;
if (Win32.CryptDecodeObject(ENCODING_TYPE,
RSA_CSP_PUBLICKEYBLOB,
encodedpubkey, (uint)encodedpubkey.Length, 0, null, ref
blobbytes))
{
publickeyblob = new byte[blobbytes];
Win32.CryptDecodeObject(ENCODING_TYPE,
RSA_CSP_PUBLICKEYBLOB,
encodedpubkey, (uint)encodedpubkey.Length, 0,
publickeyblob, ref blobbytes);
}
else
{
throw new Exception("Could not decode publickeyblob from
certificate publickey");
}
PUBKEYBLOBHEADERS pkheaders = new PUBKEYBLOBHEADERS();
int headerslength = Marshal.SizeOf(pkheaders);
IntPtr buffer = Marshal.AllocHGlobal(headerslength);
Marshal.Copy(publickeyblob, 0, buffer, headerslength);
pkheaders = (PUBKEYBLOBHEADERS)Marshal.PtrToStructure(buffer,
typeof(PUBKEYBLOBHEADERS));
Marshal.FreeHGlobal(buffer);
//----- Get public exponent -------------
byte[] exponent = BitConverter.GetBytes(pkheaders.pubexp);
//little-endian ordered
Array.Reverse(exponent); //convert to big-endian order
//----- Get modulus -------------
int modulusbytes = (int)pkheaders.bitlen / 8;
byte[] modulus = new byte[modulusbytes];
try
{
Array.Copy(publickeyblob, headerslength, modulus, 0,
modulusbytes);
Array.Reverse(modulus); //convert from little to big-
endian ordering.
}
catch (Exception)
{
throw new Exception("Problem getting modulus from
publickeyblob");
}
RSAParameters parms = new RSAParameters();
parms.Modulus = modulus;
parms.Exponent = exponent;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(parms);
return rsa;