public void ProcessRequest(HttpContext context)
{
var appid = ConfigurationManager.AppSettings["WeiXinAppId"] ?? string.Empty;
var secret = ConfigurationManager.AppSettings["WeiXinSecret"]??string.Empty;
var weburl = ConfigurationManager.AppSettings["WebUrl"]??"您的网址";
string errorCode = "微信登录过期,请重新登录!";
var code = context.Request.QueryString["Code"];
if (string.IsNullOrEmpty(code))
{
var url =
string.Format(
"https://open.weixin.qq.com/connect/qrconnect?appid={0}&redirect_uri={1}?referer_key={2}&response_type=code&scope=snsapi_login", appid,StringFilter.UrlEncode(string.Format("{0}/Api/Oauth/weixin/login/UserAuth.ashx", weburl)),secret);
context.Response.Redirect(url);
}
else
{
var client = new System.Net.WebClient();
client.Encoding = System.Text.Encoding.UTF8;
var url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);
var data = client.DownloadString(url);
var serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<Dictionary<string, string>>(data);
string accessToken;
if (!obj.TryGetValue("access_token", out accessToken))
return;
var opentid = obj["openid"];
url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, opentid);
data = client.DownloadString(url);
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余3页未读,立即下载
- 1
- 2
- 3
- 4
- 5
- 6
前往页