支付宝 网页授权 获取用户信息(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);
        }

        /// 
        /// 记录日志到文件
        /// 
        /// 
        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下载地址

你可能感兴趣的:(支付宝用户信息,C#)