登陸 支付寶開放平臺,建立應用json
進入應用api
在應用信息裏設置兩處,受權回調只須要到域名便可。app
接口加簽方式以下圖code
使用「支付寶密鑰生成器」生成,以下圖orm
將公鑰複製到 接口加簽方式 的公鑰字符。htm
程序:blog
String auth_code = context.Request.QueryString["auth_code"];
String stateStr = String.Empty;
if (String.IsNullOrEmpty(auth_code)) //若是爲空表示要獲取
{
String ParkID = context.Request.QueryString["sjh"];
String DeviceID = context.Request.QueryString["deviceid"];
String PayModel = context.Request.QueryString["paymodel"];//支付方式:場內(PayInPark)、出口(PayOutPark)token
stateStr = String.Format("sjh={0}&deviceid={1}&paymodel={2}", ParkID, DeviceID, PayModel);接口
//獲取auth_code
String redirect_uri = "http://www.zftong.cn/Cn.Ubingo/AlipayAuth/GetAlipayAuth.ashx";
redirect_uri = HttpUtility.UrlEncode(redirect_uri);
String accessUrl = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={0}&scope=auth_base&redirect_uri={1}&state={2}";
String accUrl = String.Format(accessUrl, AlipayConfig.APP_ID, redirect_uri, DESEncrypt.Encrypt(stateStr));
context.Response.Redirect(accUrl, true);
context.Response.End();
return;
}ip
IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.Pri_Key, "json", "1.0", "RSA2", AlipayConfig.Pub_Key, "GBK", false);
//獲取user_id和access_token
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.GrantType = "authorization_code";
request.Code = auth_code;
AlipaySystemOauthTokenResponse responseAccess_token = client.Execute(request);
//獲取用戶信息 注:在獲取auth_code時,若是是auth_base沒法獲取用戶信息,須要auth_user //AlipayUserInfoShareRequest requestUserinfo = new AlipayUserInfoShareRequest(); //AlipayUserInfoShareResponse responseUserinfo = client.Execute(requestUserinfo, responseAccess_token.AccessToken); //String UserName = responseUserinfo.NickName;