获取支付宝授权用户信息

登录 支付宝开放平台,创建应用

进入应用

在应用信息里设置两处,授权回调只需要到域名即可。

接口加签方式如下图

使用“支付宝密钥生成器”生成,如下图

将公钥复制到 接口加签方式 的公钥字符。

程序:

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)

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;
}


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;

原文地址:https://www.cnblogs.com/KimhillZhang/p/11551848.html