C#中,小程序和公众号的登录流程

发布于 2023-03-29  534 次阅读


微信登录流程

一、小程序登录

小程序发起登录授权,获取到jscode和用户加密信息后,可以通过接口进行微信登录

1.请求微信地址:https://api.weixin.qq.com/sns/jscode2session 并带上appid秘钥和jscode,获取到openid
2.获取到openid以后,可以通过返回的session_key进行解密小程序获取到的用户信息encryptedData从而获取到unionid
3.通过openid和unionid,进行用户注册与微信关系的绑定

public IHttpActionResult WeChatLoginByJsCode(){
    string JSCODE=GetApiString("jscode");
    string encryptedData=GetApiString("encryptedData");//加密的数据
    string iv=GetApiString("iv");//用于解密用的加密项量

    //可选参数================用于绑定用户资料
    string avatarUrl=GetApiString("avatarUrl");;//头像
    string nickName=GetApiString("nickName");;//微信昵称
    string gender=GetApiString("gender");;//性别

    string jscode2sessionUrl="https://api.weixin.qq.com/sns/jscode2session";
    jscode2sessionUrl+='?grant_type=authorization_code';//固定的值
    jscode2sessionUrl+='&js_code='+JSCODE;//加入jscode
    jscode2sessionUrl+='&appid=小程序的appid';//加入小程序appid
    jscode2sessionUrl+='&secret=小程序秘钥';//加入小程序秘钥

    //通过流的方式,获取到微信接口返回数据
    string resultJson=string.Empty;
    using (Stream s_re = WebRequest.Create(Url).GetResponse().GetResponseStream())
    {
        using (StreamReader s_reader = new StreamReader(s_re, Encoding.UTF8))
        {
            resultJson = s_reader.ReadToEnd();
        }
    }

    //将返回的数据转为字典
    Dictionary<string, string> dicResult = JsonHelper.JsonToObject<Dictionary<string, string>>(resultJson);
    if(!dicResult.ContainsKey('openid')) 
    {
        //只要成功,必定有openid,如果没有则说明失败
        //...失败业务处理
        return;
    }
    string openid=dicResult['openid'].ToString();
    string session_key=dicResult['session_key'].ToString();//用于解密参数的键

    //到了这里,其实微信登录的过程就已经完成了,下面的过程都是用于解密用户的UNIONID和注册的步骤
    stirng unionid="";//开放平台绑定ID
    if(!string.IsNullOrWhiteSpace(encryptedData)) 
    {
        //解密信息获取出用户的
         string result = AESDecrypt(encryptedData, session_key, iv);
         dicResult = JsonHelper.JsonToObject<Dictionary<string, string>>(result);
         unionid=dicResult["unionid"].ToString();
    }

    //进行注册
    //...

}

二、公众号登录

前端进行页面授权获取到code以后,通过服务端接口进行微信登录

1.请求微信的oauth2接口,通过code获取到微信授权access_token
2.通过access_token和openid获取到微信用户信息
2.通过微信登录信息,进行注册/登录

public IHttpActionResult WeChatLoginByCode()
{
    string code= GetApiString("code");
    string appid="";//微信公众号appid
    string secretKey="";//微信公众号秘钥

    //微信授权地址
    string url="https://api.weixin.qq.com/sns/oauth2/access_token";
    url+="?grant_type=authorization_code";
    url+="&appid="+appid;//拼接appid
    url+="&secret="+secretKey;//拼接秘钥
    url+="&code="+code;//拼接code

    //发起请求,获取到授权返回结果
    HttpHelper hp = new HttpHelper();
    HttpResult httpResult = hp.GetHtml(url);
    string result = httpResult.Html;
    //转换为字典
    Dictionary<string, string> dicResult = JsonHelper.JsonToObject<Dictionary<string, string>>(result);
    string access_token=dicResult["access_token"].ToString();//返回结果里的令牌
    string openid=dicResult["openid"].ToString();//返回结果里的用户openid

    /*
        这里获取到的access_token,可以进行服务端缓存起来,在有效期内,无需再次使用
    */

    //发起请求,获取用户信息和UNIONID
    string infoUrl="https://api.weixin.qq.com/sns/userinfo";
    infoUrl+="?lang=zh_CN";//中文
    infoUrl+="&access_token="+access_token;//拼接令牌
    infoUrl+="&openid="+openid;//拼接用户标识

    httpResult = hp.GetHtml(infoUrl);
    result = httpResult.Html;
    //转为字典
    Dictionary<string, string> dicUserInfoResult = JsonHelper.JsonToObject<Dictionary<string, string>>(result);

    string unionid = dicUserInfoResult["unionid"].ToString();//只有在将公众号绑定到微信开放平台帐号后,才会出现该字段。
    string nickname = dicUserInfoResult["nickname"].ToString();
    string sex = dicUserInfoResult["sex"].ToString();
    string province = dicUserInfoResult["province"].ToString();
    string city = dicUserInfoResult["city"].ToString();
    string country = dicUserInfoResult["country"].ToString();
    string headimgurl = dicUserInfoResult["headimgurl"].ToString();

    //注册登录逻辑处理...
    //...
}
啊~~~~~~~~~
最后更新于 2023-03-29