微信授权登陆

官方API: 打开连接

这次记录的主要是----Web通过微信授权登陆功能

1、项目中引用 API 基类其中包含      前去github 代码下载

                                    WeiXinOAuth.cs、WeiXinHelper.cs、WeiXinAccessTokenResult.cs、WeiXinAccessTokenModel.cs

                  底层必要参数: AppId:微信公众平台 —  基本配置   ,AppKey: ( 同上 )                    CallbackUrl:回调通知页面  (需要设置      微信公众平台 — 公众号设置—网页授权域名)

2、新建页面   Index.aspx

protected WeiXinOAuth WXOAuth = new WeiXinOAuth();
string Url = WXOAuth.GetWeiXinCode(AppId, AppKey, HttpUtility.UrlEncode(CallbackUrl), true);
if (!string.IsNullOrEmpty(Url))
        {
            this.Response.Redirect(Url);
        }

3、Notify.aspx    //微信第一次握手后得到的code 和state

        Code = GetParams("code");
        State = GetParams("state");
        if (!string.IsNullOrEmpty(Code) && Code != "authdeny")
        {
            #region 获取微信用户信息
            //获取微信的Access_Token(第二次微信握手)
            WeiXinAccessTokenResult modelResult = WXOAuth.GetWeiXinAccessToken(AppId, AppKey, Code);
            //获取微信的用户信息(第三次微信握手)
            WeiXinHelper.WeiXinUserInfoResult userInfo = WXOAuth.GetWeiXinUserInfo(modelResult.SuccessResult.access_token, modelResult.SuccessResult.openid);
            //用户信息(判断是否已经获取到用户的微信用户信息)
        
            if (userInfo.Result && userInfo.UserInfo.openid != "")
            {
                //业务记录所需数据和操作      跳转到指定页面
}
        }

基本上就没问题了。

原文地址:https://www.cnblogs.com/longm/p/6638694.html