支付寶 網頁授權 獲取用戶信息(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();
        }

    }
}

需要注意:公鑰私鑰一共2對(應用公鑰私鑰,支付寶公鑰私鑰),上面需要填寫的是應用私鑰和支付寶公鑰,這個很容易搞混!

C#獲取支付寶用戶信息,基於官方SDK寫的一個小DEMO,有什麼問題可以聯繫我QQ:136891488

demo下載地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章