一、首先在某微信平台下配置OAuth2.0受权回调页面:数据库

二、经过appid构造url获取微信回传code值(appid可在微信平台下找到)json
1)、微信不弹出受权页面url:api
A、code回传到页面wxProcess2.aspx,不带参数数组
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
B、code回传到页面wxProcess2.aspx,带参数reurl,即wxProcess2.aspx得到code的同时,也能获取reurl的值,具体以下:微信
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
2)、微信弹出受权页面url:须要用户受权,才能获取code及后面须要获取的用户信息微信开发
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
说明:微信是否弹出受权页面url的区别只在一个参数scope,不弹出微信受权页面:scope=snsapi_base,弹出微信受权页面:scope=snsapi_userinfo。app
微信受权页面以下:函数

三、经过appid、secret、code构造url,获取微信用户的openid和access token。appid、secret可在微信平台下找到,code已在上面方法中获取并回传。具体访问url:url
四、经过openid、access token获取用户信息,具体访问url:spa
说明:主要经过访问微信的3个url地址并回传数据,获取微信用户基本信息
=================================================================================================================================
具体代码:
一、获取微信code处理页面:wxProcess.aspx
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string reurl = "";
-
- if (Request.QueryString["reurl"] != null && Request.QueryString["reurl"] != "")
- {
- reurl = Request.QueryString["reurl"].ToString();
- }
- else
- {
- reurl = "http://www.csdn.net";
- }
- string code = "";
-
- if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
- {
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
- }
- else
- {
-
- Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=http://localhost:8888/wxProcess2.aspx?reurl=" + reurl + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); }
- }
- }
二、获取微信code值回传到本身的页面wxProcess2.aspx:
输出微信用户信息: