using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace alipayGetUserinfo
{
public partial class _default : System.Web.UI.Page
{
public string Userid;
public string Address;
public string UserName;
public string Auth_code;
protected void Page_Load(object sender, EventArgs e)
{
string appid = "";
string YOUR_PRIVATE_KEY = "";
string ALIPAY_PUBLIC_KEY = "";
string ENCODED_URL = "";
Auth_code = Request.QueryString["auth_code"];
if (string.IsNullOrEmpty(Auth_code))
{
var url = string.Format("https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={0}&scope=auth_user&redirect_uri={1}", appid, ENCODED_URL);
Response.Redirect(url);
}
IAopClient client = new DefaultAopClient("https://openapi.alipay.com/gateway.do", appid, YOUR_PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, "GBK", false);
AlipaySystemOauthTokenRequest requestAccess_token = new AlipaySystemOauthTokenRequest();
requestAccess_token.GrantType = "authorization_code";
requestAccess_token.Code = Auth_code;
AlipaySystemOauthTokenResponse responseAccess_token = client.Execute(requestAccess_token);
Userid = responseAccess_token.AlipayUserId;
AlipayUserInfoShareRequest requestUserinfo = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse responseUserinfo = client.Execute(requestUserinfo, responseAccess_token.AccessToken);
UserName = responseUserinfo.NickName;
Address = responseUserinfo.City;
}
public static void WriteLog(string strLog)
{
string sFilePath = HttpRuntime.AppDomainAppPath.ToString() + "/Log/" + DateTime.Now.ToString("yyyyMM");
string sFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log";
sFileName = sFilePath + "\\" + sFileName;
if (!Directory.Exists(sFilePath))
{
Directory.CreateDirectory(sFilePath);
}
FileStream fs;
StreamWriter sw;
if (File.Exists(sFileName))
{
fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
}
sw = new StreamWriter(fs);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " --- " + strLog);
sw.Close();
fs.Close();
}
}
}
需要注意:公钥私钥一共2对(应用公钥私钥,支付宝公钥私钥),上面需要填写的是应用私钥和支付宝公钥,这个很容易搞混! C#获取支付宝用户信息,基于官方SDK写的一个小DEMO,有什么问题可以联系我QQ:136891488
demo下载地址