110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zmop.Api;
using Zmop.Api.Request;
using Zmop.Api.Response;
using Zmop.Api.Util;
namespace opensdk_dotnet4._0
{
class Program
{
//芝麻信用网关地址
private const string gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
//芝麻 RSA 公钥
private const string zhimaPublicKey = "***";//这里隐去了真实的key
//商户 RSA 私钥
private const string privateKey = "***";//这里隐去了真实的key
//数据编码格式
private const string charset = "UTF-8";
////芝麻分配给商户的 appId
private const string appId = "5010001";
public static void testZhimaCustomerCertificationQuery()
{
DefaultZmopClient client = new DefaultZmopClient(gatewayUrl, appId, privateKey, zhimaPublicKey,charset);
ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
request.SetChannel("apppc");
request.SetPlatform("zmop");
request.BizNo = "ZM201705173000000323200000189778"; //--------------必要参数-------这是什么意思---- ----
ZhimaCustomerCertificationQueryResponse response = client.Execute(request);
ZhimaCustomerCertificationQueryResponse resp = response;
}
static void Main(string[] args)
{
testZhimaCustomerCertificationQuery();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Zmop.Api;
using Zmop.Api.Request;
using Zmop.Api.Response;
using System.Data;
namespace WeiXin_PcShare
{
public partial class Face : System.Web.UI.Page
{
//以下代码仅用于微信公众号
#region 公共定义
//芝麻信用网关地址
private const string gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
//芝麻 RSA 公钥
private const string zhimaPublicKey = "****";//填你真实的key
//商户 RSA 私钥
private const string privateKey = "********";//填你真实的key
//数据编码格式
private const string charset = "UTF-8";
//芝麻分配给商户的 appId
private const string appId = "3000000011";//********APPID 必须**********//填你真实的
//产品码
private static string bizNo = "";
//用户码
private static string openID = "";
//定义取到的服务器人脸识别URL
private static string checkFaceUrl = "";
//取得姓名
private static string userName = "";
//取得身份证号
private static string userCard = "";
#endregion
#region 公共芝麻信用方法
//生成流水号
public static string GetSerialNumber()
{
string SN = DateTime.Now.ToString("yyyyMMddHHmmssffff"); //18位
return SN + "000000000001";
}
//取得姓名和身份证号
public static void getNameCard()
{
// HttpContext.Current.Session["openid"] = "oOVYE0lbGniWsVc_Jes_u7-DO5aM";//测试代码,正式去除
if (HttpContext.Current.Session["openid"] == null)
{
Pub.URL("Default.aspx");
}
//数据库取得用户名和身份证号
string openids = HttpContext.Current.Session["openid"].ToString();
if (!string.IsNullOrWhiteSpace(openids))
{
string sql = "Select top 1 Name,Card from Member where Openid='" + openids + "'";
DataSet ds = DBHelper.GetDataSet(sql);
if (ds != null && ds.Tables[0].Rows.Count >= 1)
{
userName = ds.Tables[0].Rows[0][0].ToString();
userCard = ds.Tables[0].Rows[0][1].ToString();
}
}
}
//1.1发起认证初始化--取得BizNo
public static void testZhimaCustomerCertificationInitialize()
{
ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
//request.SetChannel("apppc");
request.SetPlatform("zmop");
request.TransactionId = GetSerialNumber(); //必要参数 30位长的不重复数
request.ProductCode = "w1010100000000002978"; //必要参数 --- 固定的产品码
request.BizCode = "FACE"; //必要参数
request.IdentityParam = "{\"identity_type\": \"CERT_INFO\", \"cert_type\": \"IDENTITY_CARD\", \"cert_name\": \"" + userName + "\", \"cert_no\":\""+userCard+"\"}"; //必要参数
//request.MerchantConfig = "{\"need_user_authorization\":\"false\"}";
request.ExtBizParam = "{}"; //必要参数
DefaultZmopClient client = new DefaultZmopClient(gatewayUrl, appId, privateKey, zhimaPublicKey, charset);
ZhimaCustomerCertificationInitializeResponse response = client.Execute(request);
ZhimaCustomerCertificationInitializeResponse resp = response;
//取得产品码
if (response != null)
bizNo = response.BizNo;
}
//2.1开始认证------取得sign签名URL
public static void testZhimaCustomerCertificationCheck()
{
DefaultZmopClient client = new DefaultZmopClient(gatewayUrl, appId, privateKey, zhimaPublicKey, charset);
ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
request.SetChannel("apppc");
request.SetPlatform("zmop");
request.BizNo = bizNo; //******** 必须*********
request.ReturnUrl = "http://www.taobao.com"; //完成后跳转地址,记得在此地址中做一个唤醒微信的schema,//填你真实的URL,支付宝验证人脸后返回的页面,可以是自己的页面
checkFaceUrl = client.generatePageRedirectInvokeUrl(request); //测试方法,用url生成二维码,然后用微信扫描后打开
}
//5.1发起认证查询,看用户是否已经做过实名,每一个实名的操作都会有一个不同的BizNo,这个要存起来.
public static void testZhimaCustomerCertificationQuery()
{
DefaultZmopClient client = new DefaultZmopClient(gatewayUrl, appId, privateKey, zhimaPublicKey, charset);
ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
request.SetChannel("apppc");
request.SetPlatform("zmop");
request.BizNo = bizNo; //必要参数
ZhimaCustomerCertificationQueryResponse response = client.Execute(request);
ZhimaCustomerCertificationQueryResponse resp = response;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
//取得姓名和身份号
getNameCard();
if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(userCard))
{
//1.1初始化并取得BizNo业务号
testZhimaCustomerCertificationInitialize();
//2.1取得服务器的认证URL,进入URL后,需要在浏览器中打开并调用支付宝完成人脸识别后,再唤起微信,并返回公众号个人中心
testZhimaCustomerCertificationCheck();
//判断URL并跳转
if (!string.IsNullOrWhiteSpace(checkFaceUrl))
Response.Redirect(checkFaceUrl);
}
else
{
Pub.Msg("请完善好实名信息后,再做人脸识别!","RealNameBind.aspx");
}
}
}
}
//方法1
public static void testZhimaCustomerCertificationQuery()
{
DefaultZmopClient client = new DefaultZmopClient(gatewayUrl, appId, privateKey, zhimaPublicKey, charset);
ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
request.SetChannel("apppc");
request.SetPlatform("zmop");
request.BizNo = "ZM201705173000000323200000189778"; //******** 必须*********
ZhimaCustomerCertificationQueryResponse response = client.Execute(request);
ZhimaCustomerCertificationQueryResponse resp = response;
if (response.Body != null)
response.Body.ToString();
}
private static DefaultZmopClient GetZmopClient(ZmopSettingsSection zmop = null)
{
if (zmop == null)
{
zmop = ZmopSettingsSection.GetSection();
}
return new DefaultZmopClient(zmop.ServerUrl, zmop.AppId, zmop.PrivateKey, zmop.ZhimaPublicKey, zmop.Charset);
}
/// <summary>
/// 获取芝麻信用
/// </summary>
/// <param name="openid"></param>
/// <param name="transactionId"></param>
/// <param name="bizNo"></param>
/// <param name="zmScore"></param>
public static void GetCreditScore(string openid, string transactionId, out string bizNo, out int? zmScore)
{
var zmop = ZmopSettingsSection.GetSection();
ZhimaCreditScoreGetRequest request = new ZhimaCreditScoreGetRequest()
{
OpenId = openid,
ProductCode = zmop.ScoreProductCode,
TransactionId = transactionId
};
var response = GetZmopClient(zmop).Execute(request);
bizNo = response.BizNo;
zmScore = null;
int score;
if (int.TryParse(response.ZmScore, out score))
{
zmScore = score;
}
}
public static class ZmopHelper
{
/// <summary>
/// 查询用户是否已经授权查询芝麻信用,返回空代表未授权
/// </summary>
/// <param name="certNo"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string AuthQuery(string certNo, string name)
{
ZhimaAuthInfoAuthqueryRequest request = new ZhimaAuthInfoAuthqueryRequest()
{
IdentityType = "2",
IdentityParam = $"{{\"certNo\":\"{certNo}\",\"certType\":\"IDENTITY_CARD\",\"name\":\"{name}\"}}"
};
var response = GetZmopClient().Execute(request);
return response.Authorized ? response.OpenId : null;
}
private static DefaultZmopClient GetZmopClient(ZmopSettingsSection zmop = null)
{
if (zmop == null)
{
zmop = ZmopSettingsSection.GetSection();
}
return new DefaultZmopClient(zmop.ServerUrl, zmop.AppId, zmop.PrivateKey, zmop.ZhimaPublicKey, zmop.Charset);
}
/// <summary>
/// 获取芝麻认证令牌
/// </summary>
/// <param name="certNo"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string GetToken(string certNo, string name, string transactionId,
string bizParams = null, string sourceType = "sdk", string schemaUrl = null, string pageUrl = null)
{
var zmop = ZmopSettingsSection.GetSection();
ZhimaCustomerCertifyInitialRequest request = new ZhimaCustomerCertifyInitialRequest()
{
ContractFlag = zmop.ContractFlag,
ProductCode = zmop.ProductCode,
State = JsonConvert.SerializeObject(new Dictionary<string, string> { [certNo] = certNo, [name] = name }),
IdentityType = "BY_CERTNO_AND_NAME",
IdentityParam = $"{{\"certNo\":\"{certNo}\",\"certType\":\"IDENTITY_CARD\",\"name\":\"{name}\"}}",
TransactionId = transactionId,
SourceType = sourceType,
BizParams = bizParams,
SchemaUrl = schemaUrl,
PageUrl = pageUrl
};
var response = GetZmopClient(zmop).Execute(request);
return response.Token;
}
/// <summary>
/// 获取移动端签名所需参数
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static IDictionary<string, string> GetCertifyParams(string token)
{
ZhimaCustomerCertifyApplyRequest request = new ZhimaCustomerCertifyApplyRequest()
{
Token = token
};
var url = GetZmopClient().generatePageRedirectInvokeUrl(request);
return GetParamsFromUrl(url);
}
private static IDictionary<string,string> GetParamsFromUrl(string url)
{
IDictionary<string, string> dic = new Dictionary<string, string>();
string[] temp = url.Split('?');
string[] paramsString = temp[1].Split('&');
foreach (var attr in paramsString)
{
string[] keyAndValue = attr.Split('=');
if (("params||app_id||sign").IndexOf(keyAndValue[0]) > -1)
{
dic.Add(keyAndValue[0], keyAndValue[1]);
}
}
return dic;
}
/// <summary>
/// 获取芝麻信用
/// </summary>
/// <param name="openid"></param>
/// <param name="transactionId"></param>
/// <param name="bizNo"></param>
/// <param name="zmScore"></param>
public static void GetCreditScore(string openid, string transactionId, out string bizNo, out int? zmScore)
{
var zmop = ZmopSettingsSection.GetSection();
ZhimaCreditScoreGetRequest request = new ZhimaCreditScoreGetRequest()
{
OpenId = openid,
ProductCode = zmop.ScoreProductCode,
TransactionId = transactionId
};
var response = GetZmopClient(zmop).Execute(request);
bizNo = response.BizNo;
zmScore = null;
int score;
if (int.TryParse(response.ZmScore, out score))
{
zmScore = score;
}
}
}