1. 在微信开放平台注册开发者账号,并有一个审核已通过的网站应用,并获得相对应的AppID和AppSecret,申请通过登陆后,方可开始接入流程。
2.微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:
1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;2. 通过code参数加上AppID和AppSecret等,通过API换取access_token;3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
3.实现代码如下:
两个类对象
public class user
{
public string access_token { get; set; } //接口调用凭证
public int expires_in { get; set; } //access_token接口调用凭证超时时间,单位(秒)
public string refresh_token { get; set; } //用户刷新access_token
public string openid { get; set; } //授权用户唯一标识
public string scope { get; set; } //用户授权的作用域,使用逗号(,)分隔
public string unionid { get; set; } //当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段
}
public class Errmsg
{
public string errcode { get; set; }
public string errmsg { get; set; }
}
1. 若access_token已超时,那么进行refresh_token会获取一个新的access_token,新的超时时间;
2. 若access_token未超时,那么进行refresh_token不会改变access_token,但超时时间会刷新,相当于续期access_token。
refresh_token拥有较长的有效期(30天),当refresh_token失效的后,需要用户重新授权。
public ActionResult WxLogin(string code,string state)
{
try
{
if (!string.IsNullOrWhiteSpace(code))
{
string Url = "https://api.weixin.qq.com/sns/oauth2/access\_token?appid=\*\*\*\*\*\*&secret=\*\*\*\*\*\*\*&code=" + code + "&grant_type=authorization_code";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
//转为json对象
user u = Common.JsonHelper.JsonDeserialize
if (u != null)
{
Url = "https://api.weixin.qq.com/sns/oauth2/refresh\_token?appid=\*\*\*\*\*\*&grant\_type=refresh\_token&refresh\_token=" + u.refresh_token;
request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
response = (HttpWebResponse)request.GetResponse();
myResponseStream = response.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
retString = myStreamReader.ReadToEnd();
user us = Common.JsonHelper.JsonDeserialize
TUsers user = new TUsersLogic().GetTUsers(new TUsers() { OpenIDTwo = us.openid });
if (user != null && user.ID > 0)
{
System.Web.HttpCookie cookie = new System.Web.HttpCookie("CurrentUser-" + "ID");
cookie.Value = user.ID.ToString();
cookie.Expires = DateTime.Now.AddDays(7);
System.Web.HttpContext.Current.Response.AppendCookie(cookie);
return Redirect("/home/index");
}
return View();
}
else
{
Errmsg emsg= Common.JsonHelper.JsonDeserialize
return Json(new { errcode = emsg.errcode, errmsg = emsg.errmsg }, JsonRequestBehavior.AllowGet);
}
}
string url = Common.UrlHelper.CurrentUrl;
return Redirect("https://open.weixin.qq.com/connect/qrconnect?appid=\*\*\*\*\*\*\*&redirect\_uri=" + url + "&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect");
}
catch (Exception e)
{
return Json(new { errormessage = e.Message });
}
}
请求参数及链接就不一一写了,参考链接:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir\_list&t=resource/res\_list&verify=1&id=open1419316505&token=&lang=zh\_CN