支付宝 网页授权 获取用户信息(C#)

发布时间:2024-11-19 07:23

网购时使用第三方支付,如支付宝或微信支付,保障权益 #生活常识# #购物消费技巧# #网购防骗知识#

最新推荐文章于 2024-08-22 03:59:26 发布

zongwen_wu 于 2018-04-13 13:16:16 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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下载地址

网址:支付宝 网页授权 获取用户信息(C#) https://www.yuejiaxmz.com/news/view/132937

相关内容

支付宝生活号建议装修方案sop+消费模版配置
用旧物回收玩“种树”,联合国点赞5亿支付宝蚂蚁森林用户
贵阳支付宝生活出行礼包领取(时间+入口+内容)
我们在日常生活中网上支付时,应该采取哪些安全防范措施()
支付宝内容创作平台
支付宝生活程序软件开发
mysql (8)=====用户授权管理
支付宝生活软件程序开发公司
四川家电补贴在云闪付领取+使用指南
支付宝登陆

随便看看