


Detailed explanation of using .NET WeChat development on PC to implement scan code registration and login functions
This article mainly introduces the relevant information on the implementation of the PC-side WeChat code scanning registration and login functions developed by .NET WeChat. It is very good and has reference value. Friends in need can refer to it
1. Preface
Let me first state that the focus of this article is the implementation idea. The code and database design are mainly to show the idea. If there are strict requirements for code efficiency Do not copy the items.
I believe that anyone who has done WeChat development has done a lot of authorization, but generally speaking we do more authorization for mobile websites, to be precise, we do it under WeChat. an authorization. One problem I encountered today is that the project supports WeChat and PC, and registration is open. It is required that after registering on the PC side or on the WeChat side, you can log in on the other side. In other words, whether it is PC or WeChat, "you are you" (relevant in some way).
2. Looking for solutions
#Thinking in the traditional way, WeChat can completely register through authorization , but on the PC side, the traditional method is nothing more than filling in your mobile phone number, or email, etc. If you use this method to register, the following problems will arise
1. I first authorize the registration on WeChat, then if I want to log in to the PC, I still have to register.
The solution to this problem can be: after WeChat authorizes registration, it "forces" users to fill in basic information, such as mobile phone number and email. In this way, we can generate the account and password for the user to log in on the PC in some way. For example, use the user's nickname as the account number, the mobile phone number as the password, and so on.
Disadvantages: The user experience is not good, and there are security risks. After all, your WeChat nickname, email or mobile phone number are all exposed.
2. If I register on the PC side first, how do I associate the mobile side with WeChat authorization?
Of course, there is always a solution to every problem. The idea is as follows:
Option 1: After the user registers on the PC, it is "forced" that the user must fill in the WeChat nickname. Use this as the association condition when authorizing WeChat. But unfortunately, the WeChat nickname can be changed. How can it be used for association if it is not the only one? The plan died in one fell swoop.
Option 2: After authorization on the WeChat side and registration on the PC side, users are "forced" to fill in their mobile phone number as a link. This creates a problem. We must ensure that the user’s mobile phone is authentic. No problem. This can be achieved through mobile phone verification code (the same is true for email). But let’s assume the following situation. If I have two mobile phone numbers, fill in one when registering on PC and fill in the other when registering on WeChat. Is it related? The answer is unfortunately. Furthermore, after I registered on the PC side, I didn’t fill it in (the reason why I forced double quotes), and then I used the WeChat side to authorize and log in. Well, at this point there will be two pieces of data waiting for you to find a way to correlate them, which is a typical developer digging his own hole. This approach works to some extent, but the rigor is unacceptable to developers.
3. Solution to return to the origin
Analysis: Since the above solutions have problems, let’s first They are all cast aside. To sort out our thoughts, let us return to the root of the problem. The related question requires a unique identifier. The unique identifier is just like our ID card number. When we apply for a credit card, an ID card is required. When purchasing a number card under the real-name system, an ID card is required. Assuming we are the system administrator, then I can definitely find out your mobile phone number and bank card number through your ID number.
#After having the above ideas, what we need to do is to find a unique identifier as an association. There is an important role in WeChat openid. It has the same function as the ID card number we mentioned above. The WeChat account uniquely identifies a certain public account.
Anyone who has done WeChat development should have no problem getting the WeChat authorization from openid. The question is how to implement the PC side to get the openid when registering or logging in. The author's implementation ideas are as follows. Register on PC, or display a QR code when logging in to guide users to scan the code using WeChat to jump to the authorization page. There is one most critical detail in this step. Please bring a unique authorization code (authCode) with the QR code. Imagine if the user authorizes us, we can write the openid and authCode to the database. Then we can obtain the openid associated with authCode through an API on the PC side. If we do this, we can know who is currently scanning the QR code to register or log in on the PC (register if you are not registered, log in directly if you are registered). Did it suddenly feel so easy? If you think the text is more abstract, please see the following illustration
PC WeChat QR code scanning login process
##Core Code
Scan code to log in Page backend code
public ActionResult Login() { //如果已登录,直接跳转到首页 if (User.Identity.IsAuthenticated) return RedirectToAction("Index", "Home"); string url = Request.Url.Host; string uuid = Guid.NewGuid().ToString(); ViewBag.url = "http://" + url + "/home/loginfor?uuid=" + uuid;//构造授权链接 ViewBag.uuid = uuid;//保存 uuid return View(); }
jQuery('#qrcode').qrcode({ render : "table", text : "http://baidu.com" });
<!--生成二维码的容器 p--> <p id="qrcode-container"> </p> <script src="~/Plugins/Jquery/jquery-1.9.1.min.js"></script> <script src="~/Plugins/jquery-qrcode/jquery.qrcode.min.js"></script> <script> jQuery(function () { //生成二维码 jQuery('#qrcode-container').qrcode("@ViewBag.url"); //轮询判断用户是否授权 var interval = setInterval(function () { $.post("@Url.Action("UserLogin","Home")", { "uuid": "@ViewBag.uuid" }, function (data, status) { if ("success" == status) { //用户成功授权=>跳转 if ("success" == data) { window.location.href = '@Url.Action("Index", "Home")'; clearInterval(interval); } } }); }, 200); }) </script>
public string UserLogin(string uuid) { //验证参数是否合法 if (string.IsNullOrEmpty(uuid)) return "param_error"; WX_UserRecord user = db.WX_UserRecord.Where(u => u.uuId == uuid).FirstOrDefault(); if (user == null) return "not_authcode"; //写入cookie FormsAuthentication.SetAuthCookie(user.OpenId, false); //清空uuid user.uuId = null; db.SaveChanges(); return "success"; }
WeChat authorization Action
public ActionResult Loginfor(string uuid) { #region 获取基本信息 - snsapi_userinfo /* * 创建微信通用类 - 这里代码比较复杂不在这里贴出 * 迟点我会将整个 Demo 稍微整理放上 Github */ WechatUserContext wxcontext = new WechatUserContext(System.Web.HttpContext.Current, uuid); //使用微信通用类获取用户基本信息 wxcontext.GetUserInfo(); if (!string.IsNullOrEmpty(wxcontext.openid)) { uuid = Request["state"]; //判断数据库是否存在 WX_UserRecord user = db.WX_UserRecord.Where(u => u.OpenId == wxcontext.openid).FirstOrDefault(); if (null == user) { user = new WX_UserRecord(); user.OpenId = wxcontext.openid; user.City = wxcontext.city; user.Country = wxcontext.country; user.CreateTime = DateTime.Now; user.HeadImgUrl = wxcontext.headimgurl; user.Nickname = wxcontext.nickname; user.Province = wxcontext.province; user.Sex = wxcontext.sex; user.Unionid = wxcontext.unionid; user.uuId = uuid; db.WX_UserRecord.Add(user); } user.uuId = uuid; db.SaveChanges(); } #endregion return View(); }
Finally, attach the database table design
The above is the detailed content of Detailed explanation of using .NET WeChat development on PC to implement scan code registration and login functions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If you encounter the error "The access point is temporarily full" when connecting to a Wi-Fi router or mobile hotspot on your Windows 11/10 PC, this is usually caused by network overload or too many connected devices. In order to solve this problem and successfully connect to the Internet, you can try the following methods: 1. Wait for a while for other devices to disconnect before trying to connect again. 2. Restart your Wi-Fi router or mobile hotspot to clear the network cache and reassign the IP address. 3. Make sure your PC’s Wi-Fi adapter driver is up to date, check for updates through Device Manager. 4. Try to connect at different times. Avoiding peak hours may have better connection opportunities. 5. Consider adding AccessP

PC is a common abbreviation that stands for "Personal Computer". A personal computer is a ubiquitous computing device that can be used to process and store data, run software programs, and connect to the Internet. In an era of digitization and informationization, the personal computer is not only a tool, but also a window to connect to the world. It is also an important tool for people to acquire knowledge, enrich their lives and achieve personal development.
![Windows PC keeps booting into BIOS [Fix]](https://img.php.cn/upload/article/000/887/227/171012121854600.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
If your Windows PC frequently enters the BIOS interface, this may cause difficulty in use. I'm stuck with the BIOS screen every time I turn on my computer, and restarting doesn't help. If you are facing this problem, then the solutions provided in this article will help you. Why does my computer keep booting in BIOS? Your computer's frequent restarts in BIOS mode may be caused by a variety of reasons, such as improper boot sequence settings, damaged SATA cables, loose connections, BIOS configuration errors, or hard drive failures, etc. Fix Windows PC Keeps Booting into BIOS If your Windows PC keeps booting into BIOS, use the fix below. Check your boot order and re-plug the

SamsungFlow is a convenient and practical tool that allows you to easily connect your Galaxy phone to your Windows PC. With SamsungFlow, you can conveniently share content between devices, sync notifications, mirror smartphones, and more. This article will introduce how to use SamsungFlow on a Windows computer. How to use Smartphone Streaming on Windows PC To use SamsungFlow to connect your Windows PC and Galaxy Phone, you need to ensure that your Galaxy smartphones and tablets are running Android 7.0 or higher, and your Windows PC is running Windows 10 or higher.

This article will teach you how to download all OneDrive files to your PC at once. OneDrive is a powerful cloud storage platform that allows users to access their files anytime, anywhere. Sometimes, users may need to back up files locally or access them offline. Read on to learn how to do this easily. How to download all OneDrive files to PC at once? Follow these steps to download all OneDrive files to your Windows PC at once: Launch Onedrive and navigate to My Files. All files uploaded on OneDrive will be available here. Press CTRL+A to select all files, or check the checkbox to toggle selection of all items. Click on the download option at the top,

Speaking of which, we have already produced many issues of the foreign junk series, but before that, most of them were mobile phones and assembled PCs. The former has average playability, while the latter is full of uncertainty. For example, the computer we spent 300 to install last time has now entered a state of non-stop driver removal. However, "picking up rags" is what it is, and the coexistence of risks and benefits is the norm. For example, I "picked up" the ASUS ChromeBox this time. I originally wanted to make it into a Macmini (fake), but I encountered many unexpected problems during the process and failed to achieve the intended goal. In the end, I had to settle for the next best thing and choose to flash Windows on it. Although the attempt to blacken apples fell to the last step, I had a lot of fun in the whole process. And as

We know that Microsoft Windows 11 is a full-featured and attractively designed operating system. However, users have been asking for the Windows 11 Lite version. Although it offers significant improvements, Windows 11 is a resource-hungry operating system that can quickly clutter older machines to the point where they can no longer run smoothly. This article will address your most frequently asked questions about whether there is a Windows 11 Lite version and whether it is safe to download. Follow! Is there a Windows 11 Lite version? The Windows 11 Lite 21H2 version we are talking about was developed by Neelkalpa T

If 2023 is recognized as the first year of AI, then 2024 is likely to be a key year for the popularization of large AI models. In the past year, a large number of large AI models and a large number of AI applications have emerged. Manufacturers such as Meta and Google have also begun to launch their own online/local large models to the public, similar to "AI artificial intelligence" that is out of reach. The concept suddenly came to people. Nowadays, people are increasingly exposed to artificial intelligence in their lives. If you look carefully, you will find that almost all of the various AI applications you have access to are deployed on the "cloud". If you want to build a device that can run large models locally, then the hardware is a brand-new AIPC priced at more than 5,000 yuan. For ordinary people,
