微信扫码登录之最简实现( 二 )


/// <summary>/// 登录接口/// </summary>/// <param name="sessionId">客户端sessionid</param>/// <param name="code">公众号授权code</param>/// <returns>{userInfo:xxxx, token:xxxx}</returns>[HttpGet][AllowAnonymous]public async Task Login(string sessionId, string? code = null){if (string.IsNullOrEmpty(code)){string redirect_uri = HttpUtility.UrlEncode(Request.GetDisplayUrl());//授权后跳转回来Response.Redirect($"https://open.weixin.qq.com/connect/oauth2/authorize?appid={APPID}&redirect_uri={redirect_uri}&response_type=code&scope=SCOPE&state=STATE#wechat_redirect");}else{string openId = await WechatServices.GetOpenId(code);var result = adminServices.Login(openId);if (result.Code == 0){Redis_Utility.Set($"expert_admin_login_{sessionId}", result.Data, 1);string html = htmlTemp.Replace("$content", "登录成功");await Response.WriteAsync(html);}else{await Response.WriteAsync(result.Message);}}}码字辛苦,如果觉得有参考价值请点个赞,如果有看不懂的地方欢迎提问,如果有更好更简单的办法欢迎指教!





推荐阅读