Alipay website authorization to obtain user information (C#)

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)
        {
            //appid
            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);
            //获取access_token
            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;
            //WriteLog(responseAccess_token.Body);

            //获取用户信息
            AlipayUserInfoShareRequest requestUserinfo = new AlipayUserInfoShareRequest();
            AlipayUserInfoShareResponse responseUserinfo = client.Execute(requestUserinfo, responseAccess_token.AccessToken);
            UserName = responseUserinfo.NickName;
            Address = responseUserinfo.City;
            //WriteLog(responseUserinfo.Body);
        }

        /// <summary>
        /// 记录日志到文件
        /// </summary>
        /// <param name="WriteLog"></param>
        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();
        }

    }
}

Note: There are 2 pairs of public key and private key (application public key and private key, Alipay public key and private key), the above need to fill in the application private key and Alipay public key, this is easy to confuse!

C# Get Alipay user information, based on a small DEMO written by the official SDK, if you have any questions, please contact me QQ: 136891488

Demo download address

Guess you like

Origin blog.csdn.net/wu_zongwen/article/details/79927362