首页 > 其他分享 >NET7下通过code取openid

NET7下通过code取openid

时间:2023-08-25 12:13:18浏览次数:49  
标签:openid code string ret var NET7 new

 

NET7下通过code取openid 微信小程序文档:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html 其实就是取到code后再把code拼接到一个地址里再访问那个地址取到openid,

 

        /// <summary>
        /// 根据CODE取OPENID,再根据OPENID取用户实体
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("GetByCode")]
        public ApiResult GetByCode(string code)
        {
            try
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new Exception("传入code为空");
                }
                var appid = "wx3fgdsagsgds";
                var secret = "0015950fdsagdsaghfds";
                var grant_type = "authorization_code";
                var url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&grant_type=" + grant_type + "&js_code=" + code;
                ResponseInfo response = HTTP.Send(url); //nuget: AgileHttp

                RemoteReturn ret = Newtonsoft.Json.JsonConvert.DeserializeObject<RemoteReturn>(response.GetResponseContent());
                if (ret.errcode == 0)
                {
                    #region 自动注册 
                    string openid = ret.openid;
                    Model.User u = _userRepository.FirstOrDefault(a => a.Open_Id == openid);
                    if (u == null)
                    {
                        string name = "微信用户" + openid.Substring(openid.Length - 4);
                        u = new User() { Open_Id = openid, Sex = 1, Belong_job_id = "", HeadImgUrl = "/upload/face.png", Small_headimgurl = "/upload/face.png", Id_Card = "", Integral = 0, Name = name, NickName = name, Phone = "", QrCode = "", Recommend_open_id = "", Role = "普通用户", };
                        _userRepository.Insert(u);
                    }
                    #endregion
                    return new ApiResult() { flag = true, message = "成功取出用户实体", data = u };
                }
                throw new Exception(ret.errmsg);
            }
            catch (Exception ex)
            {
                return new ApiResult() { flag = false, message = ex.Message };
            }
        }

 

标签:openid,code,string,ret,var,NET7,new
From: https://www.cnblogs.com/niunan/p/17656594.html

相关文章

  • vscode 中 Markdown 粘贴图片的位置
    背景自从typora开始收费后,不少人开始寻找其他的Markdown编辑器,我觉得vscode就是一个很不错的选择虽然不能像typora在Markdown预览中编辑,但是左右布局对于一个前端工程师来说已是习以为常PS:自从github被微软收购后,github开始使用网页版的vscode编辑器......
  • LeetCode-21. 合并两个有序链表(Java)
    这是我在51CTO博客开启的写作之路,第一次正式写博客记录我在LeetCode的刷题日,希望能帮助更多的小伙伴攻面自己心仪的公司offer。如下对于 LeetCode-21.合并两个有序链表,进行全面解析并小结解题思路,同学们请参考:1.题目描述将两个升序链表合并为一个新的 升序 链表并返回。新链表......
  • LeetCode-24. 两两交换链表中的节点(Golang)
    一、前言作者:bug菌博客:CSDN、掘金、infoQ、51CTO等简介:CSDN/阿里云/华为云/51CTO博客专家,博客之星Top30,掘金年度人气作者Top40,51CTO年度博主Top12,掘金/InfoQ/51CTO等社区优质创作者,全网粉丝合计10w+,硬核微信公众号「猿圈奇妙屋」,免费领取简历模板/学习资料/大厂面试真题/职业规划......
  • C# 使用 PaddleOCRSharp 识别 图片中的文字、 使用QRCoder生成二维码
    使用PaddleOCRSharp识别图片中的文字  在只用PaddleOCRSharp之前用过另外一种识别:Tesseract。它识别速度是非常快的,但是准确率堪忧,而且使用的时候需要区分语言,这里权当一些经验交流,不是说Tesseract不好。PaddleOCRSharp资料汇总:开源工具开发者博客:https://www.cnblogs.com/ra......
  • VScode settings.json默认配置文件路径
    LinuxUbuntu:/home/${用户名}/.config/Code/User/settings.jsonWindows:C:\Users\用户名\AppData\Roaming\Code\User来源、参考:https://blog.csdn.net/cyqzy/article/details/130011314......
  • [LeetCode][198]house-robber
    ContentYouareaprofessionalrobberplanningtorobhousesalongastreet.Eachhousehasacertainamountofmoneystashed,theonlyconstraintstoppingyoufromrobbingeachofthemisthatadjacenthouseshavesecuritysystemsconnectedanditwilla......
  • SAP GUI Scripting VBA Code Snippet to Detect all IDs of the UI Elements
    '-Begin-----------------------------------------------------------------OptionExplicitDimgColl()AsStringDimjAsIntegerSubGetAll(ObjAsObject)'---------------------------------------------'-'-Recursivelycalledsubro......
  • VS插件DevExpress CodeRush v23.1 - 支持Visual Studio ARM
    DevExpress CodeRush是一个强大的VisualStudio.NET插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验。CodeRush能帮助你以极高的效率创建和维护源代码。Consume-first申明,强大的模板,智能的选择工具,智能代码分析和创新的导航以及一个无与伦比的重构集,在它们的帮助......
  • 【LeetCode动态规划#15】最长公共子序列II
    最长公共子序列(二)描述给定两个字符串str1和str2,输出两个字符串的最长公共子序列。如果最长公共子序列为空,则返回"-1"。目前给出的数据,仅仅会存在一个最长的公共子序列数据范围:0≤∣���1∣,∣���2∣≤20000≤∣str1∣,∣str2∣≤2000要求:空间复杂度�(�2)O(n2),时间复杂度�(�2)O(n2)......
  • vscode 配置
    {"workbench.colorTheme":"VisualStudioDark","editor.fontSize":16,"workbench.startupEditor":"none","editor.formatOnType":true,"workbench.settings.openDefaultSett......