您當前位置>首頁 » 新聞資(zī)訊 » 小程序相關(guān) >
支付寶小程序開發系列一: 獲取用戶手機号(.net)
發表時間:2020-9-21
發布人:葵宇科技
浏覽次數:79
第一步:開放平台上添加 獲取會員手機号的功能包(需要主賬号簽約,子(zǐ)賬号不可(kě)見)
https://open.alipay.com/operation/mini/ops/mini-summary
第二步:添加主動(dòng)觸發按鈕
getPhoneNumber:授權成功後的回調
onAuthError:拒絕授權後的回調
<button open-type="getAuthorize"> my.getPhoneNumber({
success: (res) => {
let encryptedData = JSON.parse(res.response).response;
//userObject.phoneInfo = res.response;
console.log(res.response);
//手機号解密請求
phoneAuth(res.response);
},
fail: (res) => {
console.log(res);
}
});
function phoneAuth(e){
//後台請求操作 主要是發送加密數據
}
第四步:後台解析(低版本SDK:https://download.csdn.net/download/qq_36894527/12875709)
//應用公鑰
public static string PublicKey = "";
//aes密鑰
public static string AESKey = "";
/// <summary>
/// 手機号解密
/// </summary>
/// <param name="openapiResult"></param>
/// <returns></returns>
public static string Decypt(string openapiResult, string publicKey, string aesKey)
{
//openapiResult= "{\"response\":\"9+asiPElYO6D9jVY21115q6WIe7Lobo0vIbP5D9l0hwmHERiIhu/jsMKoWX4PdJd05BUOLL0xSFazUut+4JH2udCwOaZVJcbT9/sJgMhAavSYGUDyKB3bffXRgDLRKBL+PGcnPD6JjQUgGOM9JPFxOBwmD0U0Ylo1GDagR9pjZjtAyFzAcJYDkGrOKTpsO9nTn2MFHfkUNikX/q1gPOehUiUnvb3ZqcPVs4rEb7M5xpgoBcc/eht+zpMBLFShaBpY3BtQluHxaTeEtT3a66G/SmcoJ0yBCt1z78ZQVy9pmStMgDnmVINArQz/O2XrPUakN3TszH9U1qnoJ5C8LSu/CsS0E8XApSu1iERQjKfJlg=\",\"sign\":\"KyQCYndlwsunFXOivUJGHdg88M4HyxtucizmGarsmvc+uUVOSuQlHhnYa3iKWkVxTv76Y75JJuiZP5wSqnfU0jjln27Xi0BSh2JPTF/4oIBJlrqQxsw9bVkzaGu5bkB+Tw4sZQwO8vgg8Z7f8bu6go/BWAEd5m4ds5haTQ9enH5ZDjb7S08EJbYLe9Nnz/BJbv0M1lbW60nGr5QyDFvmnE91g2U1NhAKaZewjIm/7xsFYlQ7xh68A+fEkr4TIzA7scILGncnmQ+mU3B98I5njiTzhxig+pByCGkY2vT3U6z+4VUFUuB0+bwOBNqYp++bXQs/cAhcv+7FV/yW4Qm+9w==\"}";
//小程序通(tōng)過 my.getPhoneNumber 獲取到加密數據,詳見獲取會員手機号接入文(wén)檔
string res = "";
JObject jObj = null;
//1. 獲取驗簽和(hé)解密所需要的參數
try
{
jObj = (JObject)JsonConvert.DeserializeObject(openapiResult);
}
catch (Exception ex)
{
throw new Exception("獲取驗簽和(hé)解密所需要的參數失敗", ex);
}
string signType;
if (openapiResult.Contains("sign_type"))
{
signType = jObj["sign_type"].ToString();
}
else
{
signType = "RSA2";
}
string charset;
if (openapiResult.Contains("charset"))
{
charset = jObj["charset"].ToString();
}
else
{
charset = "UTF-8";
}
string encryptType;
if (openapiResult.Contains("encrypt_type"))
{
encryptType = jObj["encrypt_type"].ToString();
}
else
{
encryptType = "AES";
}
string sign = jObj["sign"].ToString();
string content = jObj["response"].ToString();
//如(rú)果密文(wén)的
bool isDataEncrypted = !content.StartsWith("{", StringComparison.Ordinal);
bool signCheckPass = false;
//2. 驗簽
string signContent = content;
//RSA2支付寶公鑰(不是應用公鑰),在控制台-小程序-開發設置-開發設置-接口加簽方式-設置/查看
string signVeriKey = publicKey;
//填小程序設置的AES密鑰。注意:若AES密鑰中(zhōng)存在反斜杠"\\"會導緻解密報錯,需删除反斜杠或更改沒有反斜杠的AES密鑰
string decryptKey = aesKey;
//如(rú)果是加密的報文(wén)則需要在密文(wén)的前後添加雙引号
if (isDataEncrypted)
{
signContent = "\"" + signContent + "\"";
}
try
{
signCheckPass = AlipaySignature.RSACheckContent(signContent, sign, signVeriKey, charset, signType, false);
Console.WriteLine("RSA2驗簽結果:" + signCheckPass);
}
catch (Exception ex)
{
//驗簽異常, 日志
throw new Exception("驗簽失敗", ex);
}
if (!signCheckPass)
{
//驗簽不通(tōng)過(異常或者報文(wén)被篡改),終止流程(不需要做解密)
throw new Exception("驗簽失敗");
}
//3. 解密
string plainData = null;
if (isDataEncrypted)
{
try
{
plainData = AlipayEncrypt.AesDencrypt(decryptKey, content, charset);
Console.WriteLine("AES解密結果:" + plainData);
}
catch (Exception ex)
{
//解密異常, 記錄日志
throw new Exception("解密異常", ex);
}
}
else
{
plainData = content;
}
return plainData;
}