标签:comm   china   数据库   公众号   cal   response   set   har   lib   
1、首先在某微信平台下配置OAuth2.0授权回调页面:

2、通过appid构造url获取微信回传code值(appid可在微信平台下找到)
1)、微信不弹出授权页面url:
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。
微信授权页面如下:

 
3、通过appid、secret、code构造url,获取微信用户的openid和access token。appid、secret可在微信平台下找到,code已在上面方法中获取并回传。具体访问url:
 
 
 
4、通过openid、access token获取用户信息,具体访问url:
 
 
 
说明:主要通过访问微信的3个url地址并回传数据,获取微信用户基本信息
 
=================================================================================================================================
具体代码:
 
1、获取微信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");            }  
 
-         }  
 
-     }  
 
 
 
 
2、获取微信code值回传到自己的页面wxProcess2.aspx:
 
 
 
 输出微信用户信息:
微信公众平台开发—利用OAuth2.0获取微信用户基本信息
标签:comm   china   数据库   公众号   cal   response   set   har   lib   
原文地址:http://www.cnblogs.com/soundcode/p/7487741.html