3. ABP .NETCore 添加企业微信第三方登录
程序员文章站
2022-04-10 14:29:44
1.企业微信登录步骤 1.获取企业微信Token 官方文档:https://work.weixin.qq.com/api/doc#90000/90135/91039 2.通过Token 与前端传的Code 参数 调用微信API获取 访问用户身份 官方文档https://work.weixin.qq. ......
1.企业微信登录步骤
1.获取企业微信token
官方文档:
2.通过token 与前端传的code 参数 调用微信api获取 访问用户身份
官方文档
3.通过获取的企业用户信息到自己的服务器进行查询,存在就登录成功,不存在则登录失败,前提是我们先要把企业微信的用户同步到自己的服务器。
2.下图是我们需要添加或修改的文件
3下面开始我们的代码:
1.先声明微信接口返回的实体
// 获取token返回的实体
public class wechattoken { public int errcode { get; set; } public string errmsg { get; set; } public string access_token { get; set; } public int expires_in { get; set; } }
public class wechatuserinfo { public int errcode { get; set; } public string errmsg { get; set; } // 企业用户id public string userid { get; set; } // 非企业用户id public string openid { get; set; } public string deviceid { get; set; } }
2.添加企业微信第三方登录
using abp.ui; using newtonsoft.json; using system; using system.linq; using system.net.http; using system.threading.tasks; using texhong_emwx.authentication.jwtbearer; using texhong_emwx.authorization; using texhong_emwx.authorization.users; using texhong_emwx.models.wechat; using texhong_emwx.wxusers; namespace texhong_emwx.authentication.external { public class wechatminiprogramauthproviderapi : externalauthproviderapibase { /// 第三方登录名称需要与前端配置的一致 public const string providername = "enterprisewechat"; private readonly usermanager _usermanager;
/// 本地微信用户的服务。 这个需要自己添加。用于判断当前微信用户是否有权限 private readonly wxusermanager _wxusermanager; private readonly loginmanager _loginmanager; private readonly tokenauthconfiguration _configuration; private readonly iexternalauthconfiguration _externalauthconfiguration; public wechatminiprogramauthproviderapi( usermanager _usermanager, wxusermanager _wxusermanager, loginmanager _loginmanager, tokenauthconfiguration _configuration, iexternalauthconfiguration _externalauthconfiguration) { this._usermanager = _usermanager; this._wxusermanager = _wxusermanager; this._loginmanager = _loginmanager; this._configuration = _configuration; this._externalauthconfiguration = _externalauthconfiguration; } public override async task<externalauthuserinfo> getuserinfo(string code) {
// 1. 获取企业微信token wechattoken wechattoken = await this.getwechattoken();
// 2. 获取用户信息 wechatuserinfo wechatuserinfo = await this.getwechatuserid(code, wechattoken.access_token);
// 3. 通过获取的的微信用户userid并判断是否存在自己的服务器中。 wxuser wxuser = await _wxusermanager.findbyuserid(wechatuserinfo.userid); var t = wxuser == null ? new externalauthuserinfo() : new externalauthuserinfo { emailaddress = wxuser.email, surname = wxuser.abpuser.surname, providerkey = wechatuserinfo.userid, provider = providername, name = wxuser.abpuser.name }; return t; } public async task<wechattoken> getwechattoken() { var provider = _externalauthconfiguration.providers.firstordefault(p => p.name == providername); var appid = provider.clientid; var secret = provider.clientsecret; try { var httpclient = new httpclient(); httpclient.defaultrequestheaders.add("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"); httpclient.timeout = timespan.fromminutes(3); var urltoken = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={appid}&corpsecret={secret}"; string resulttoken = await httpclient.getstringasync(urltoken); wechattoken wx_token = jsonconvert.deserializeobject<wechattoken>(resulttoken); return wx_token; } catch (exception ex) { throw new userfriendlyexception("获取微信access_token失败" + ex.message); } } public async task<wechatuserinfo> getwechatuserid(string code,string token) { try { var httpclient = new httpclient(); httpclient.defaultrequestheaders.add("user-agent", "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)"); httpclient.timeout = timespan.fromminutes(3); var urlgetuserinfo = $"https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={token}&code={code}"; var resultuserinfo = await httpclient.getstringasync(urlgetuserinfo); wechatuserinfo wxuserinfo = jsonconvert.deserializeobject<wechatuserinfo>(resultuserinfo); return wxuserinfo; } catch (exception ex) { throw new userfriendlyexception("获取微信userinfo失败" + ex.message);
}
}
}
}
3.修改配置文件 appsetttings.json
"enterprisewechat": { "isenabled": "true", "appid": "", "secret": "" }
4.注入第三方登录 在webcoremodule.cs 中添加注入代码
public override void preinitialize()
{
configureexternalauthproviders();
}
public void configureexternalauthproviders() { iocmanager.register<externalloginproviderinfo>(); iocmanager.register<iexternalauthconfiguration,externalauthconfiguration>(); var externalauthconfiguration = iocmanager.resolve<externalauthconfiguration>(); if (bool.parse(_appconfiguration["authentication:enterprisewechat:isenabled"])) { externalauthconfiguration.providers.add( new externalloginproviderinfo( wechatminiprogramauthproviderapi.providername, _appconfiguration["authentication:enterprisewechat:appid"], _appconfiguration["authentication:enterprisewechat:secret"], typeof(wechatminiprogramauthproviderapi) ) ); } }
5.接下来最后一步就是修改 externalauthenticate 方法,因为里面的代码abp默认就已经我们实现了一些东西,需要根据自己实际的需要进行修改。
发布下项目测试下自动登录吧。