.net微信授权登录整理

实习生一枚,这些步骤都是大神带我的,代码是我看着大神写的,整理一下,以便学习
注释我加的,有不对的欢迎大神指导
1获取到当前页面code的值
https://open.weixin.qq.com/connect/oauth2/authorize?
appid=appid&redirect_uri=http://cashweb.**.com/WechatLogin/test.aspx/&response_type=code&scope=snsapi_base&state=123#wechat_redirect
2 APPID是固定的,secret是一样的,将code拼接进去
https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=app_secret&code=031de25486406e269f6b5cdf53f2274T&grant_type=authorization_code

把一样的序列封装起来
public class WechatEnum
{
public static string AppId = “appid”;
public static string Secret = “app_secret”;
}

3这时候用草料生成二维码之后用微信客户端进去,复制这个网页链接会发现得到一串对象如下:
因为code只有2分钟有效,所以会有错误的情况;

错误的格式:
{“errcode”:40029,”errmsg”:”invalid code, hints: [ req_id: wHkmQA0284ns23 ]”}

正确的格式:
{“access_token”:”OezXcEiiBSKSxW0eoylIeOnE2DYseB7xt1MjVAoH_8OgoAEOv3yZXehdJQjjCrUzAjeaZDkJ4BPM3LYYljvUy_iy_3vOe3DrBSZbyUJq4so2o1B1qLuy0Ws_Z3imbbuF_y6km_kB2gNfw4WZOTAk
Og”,”expires_in”:7200,”refresh_token”:”OezXcEiiBSKSxW0eoylIeOnE2DYseB7xt1MjVAoH_8OgoAEOv3yZXehdJQjjCrUzCh4
NgXVrol_J7ouUbbcnTjZEhAFbnssb2tN4A3o9TfeeUvp2t1IIuh0CQ6zxxHV_L8hCpmAwPKDu034t2pT9A”,”openid”:”okRvMvshkFpYwRCLj3WLyaretBeQ”,”scope”:”snsapi_base”,”unionid”:”oTvXNvp_ABnmleURxALWGo2IzYc0”}

将获取的这些值封装成一个对象:
public class TouchOpenIdResponse : ResponseModelBase
{
///
/// 微信返回的json数据包错误码
///
public string errcode { get; set; }
///
/// 微信返回的json数据包错误信息
///
public string errmsg { get; set; }
///
/// 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
///
public string access_token { get; set; }
///
/// access_token接口调用凭证超时时间,单位(秒)
///
public string expires_in { get; set; }
///
/// 用户刷新access_token
///
public string refresh_token { get; set; }
///
/// 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
///
public string openid { get; set; }
///
/// 用户授权的作用域,使用逗号(,)分隔
///
public string scope { get; set; }
///
/// 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段
///
public string unionid { get; set; }
}

将code传进去,然后将对象吐出来的一个方法:
public class LoginBusiness
{
public static TouchOpenIdResponse GetTokenModel(string code)
{
var responseModel = new TouchOpenIdResponse
{
IsPassed = false
};

        if (string.IsNullOrEmpty(code))
        {
            responseModel.Description = "code不能为空";
            return responseModel;//空的也返回空的对象
        }
        string wechatUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";

        string requetUrl = string.Format("{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code", wechatUrl, WechatEnum.AppId, WechatEnum.Secret, code);

 string result = HttpHelper.GetDataFromServer(requetUrl);

        //将get出来的result字符串变成TouchOpenIdResponse对象
        responseModel = JsonConvert.DeserializeObject<TouchOpenIdResponse>(result);
        responseModel.IsPassed = true;
        //返回对象
        return responseModel;
    }
}

主要aspx页面代码:
public partial class TouchLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string code = Request.QueryString[“code”];
string refid = Request.QueryString[“refid”];
var tokenModel = LoginBusiness.GetTokenModel(code);
if (tokenModel.IsPassed && string.IsNullOrEmpty(tokenModel.errcode) &&
!string.IsNullOrEmpty(tokenModel.openid))
{
Response.Redirect(string.Format(“http://m.*.com?refid={0}&openid={1}”, refid, tokenModel.openid));
}
else
{
Response.Redirect(string.Format(“http://m.*.com?refid={0}”, refid));
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值