• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Asp.Net Core 企业微信静默授权的实现

asp 搞代码 4年前 (2022-01-03) 32次浏览 已收录 0个评论

这篇文章主要介绍了Asp.Net Core 企业微信静默授权的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

企业微信接口文档

1. 构造授权网页链接

2.回调获取到 Code 通过code+access_token去 请求用户信息

3. 获取access_token

调试准备工作 –>内网穿透+域名 推荐向日葵有免费的,免费的开发测试够用了

域名的配置成可信用

上代码 Demo下载

 [ApiController] [Route("api/[controller]")] public class Auth2Controller : ControllerBase { private readonly string _agentId = "1000002"; private readonly string _secret = "Y3f8ESBIBJoC8M_FPHOlpvmghS_Nn2ceFePRVZjw9_E"; private readonly string _corpId = "wwbf72a7a059eac0f8"; /// <summary> /// 授权地址 /// </summary> private readonly string _auth2url = "https://open.weixin.qq.com/connect/oauth2/authorize"; /// <summary> /// 授权回调地址 /// </summary> private readonly string _callbackurl = "http://******.zicp.vip/auth2callback/api/Auth2/Callback"; /// <summary> /// 获取access_token地址 /// </summary> private readonly string _gettokenurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; /// <summary> /// 获取访问用户身份地址 /// </summary> private readonly string _getuserurl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo"; private readonly ILogger _logger; private readonly IHttpClientFactory _clientFactory; private readonly IMemoryCache _memoryCache; public Auth2Controller(ILogger logger, IHttpClientFactory clientFactory, IMemoryCache memoryCache) { _logger = logger; _clientFactory = clientFactory; _memoryCache = memoryCache; } [HttpGet] public IActionResult Auth2(string redirecturi) { string strurl = $"{_auth2url}?" + $"&appid={_corpId}" + $"&redirect_uri={System.Web.HttpUtility.UrlEncode(_callbackurl)}" + $"&response_type=code" + $"&scope={_secret}" + $"&agentid={_agentId}" + $"&state={System.Web.HttpUtility.UrlEncode(redirecturi)}#wechat_redirect"; return Redirect(strurl); } [HttpGet("Callback")] public async Task Callback(string code, string state) { /** 1)code只能消费一次,不能重复消费。比如说,是否存在多个服务器同时消费同一code情况。 2)code需要在有效期间消费(5分钟),过期会自动失效。 */ string access_token = await GetAccessToken(); string url = $"{_getuserurl}?access_token={access_token}&code=[code]"; HttpResponseMessage response = await _clientFactory.CreateClient().GetAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { using (var responseStream = await response.Content.ReadAsStreamAsync()) { var userinfo = JsonConvert.DeserializeObject(new StreamReader(responseStream).ReadToEnd()); int errcode = userinfo.errcode; if (errcode == 0) { //企业成员 string UserId = userinfo.UserId; //外部成员 string OpenId = userinfo.OpenId; /** userid是系统生成的可以修改一次; 所以后面的业务逻辑如果遇到错误就要重新授权一下; */ if (UserId==null) { _memoryCache.Set("UserId", OpenId); } else { _memoryCache.Set("UserId", UserId); } } else { _logger.LogError($"getuserinfo请求错误:{userinfo.errmsg}"); return Ok(); } } } return Redirect($"{System.Web.HttpUtility.UrlDecode(state)}?UserId={_memoryCache.Get("UserId")}"); } public async Task GetAccessToken() { if (_memoryCache.Get("AccessToken") == null) { string url = $"{_gettokenurl}?corpid={_corpId}&corpsecret={_secret}"; HttpResponseMessage response = await _clientFactory.CreateClient().GetAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { using (var responseStream = await response.Content.ReadAsStreamAsync()) { var access_token_result = JsonConvert.DeserializeObject(new StreamReader(responseStream).ReadToEnd()); int errcode = access_token_result.errcode; if (errcode == 0) { string access_token = access_token_result.access_token; int expires_in = access_token_result.expires_in; _memoryCache.Set("AccessToken", access_token, DateTimeOffset.Now.AddSeconds(expires_in - 10)); } else { _logger.LogError($"access_token请求错误:{access_token_result.errmsg }"); } } } } return _memo<mark style="color:transparent">来源gaodaimacom搞#^代%!码网</mark>ryCache.Get("AccessToken"); } }

以上就是Asp.Net Core 企业微信静默授权的实现的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Asp.Net Core 企业微信静默授权的实现

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址