
正文
AbpZero之企业微信---登录(拓展第三方auth授权登录)---第二步:开始逐步实现企业微信登录
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
上回分解到AbpZero的auth登录机制,这里我们开始着手逐步实现我们的auth登录。
我们新建一个类库XXXX.Web.Authentication.External
在类库下新建一个类QYWechatAuthProviderApi.cs并继承ExternalAuthProviderApiBase 由于我用的是盛派的SDK,所以还要在项目Nuget盛派的工程dll

using Abp.AspNetZeroCore.Web.Authentication.External;
using Abp.Domain.Repositories;
using Abp.Runtime.Caching;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Senparc.Weixin;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.Work.AdvancedAPIs.OAuth2;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace XXXX.Web.Authentication.External
{
public class WechatAuthProviderApi : ExternalAuthProviderApiBase
{
public const string Name = "Wechat";
private readonly ICacheManager _cacheManager;
private readonly IExternalAuthConfiguration _externalAuthConfiguration;
WeChatMiniProgramOptions _options;
JSchema schema = JSchema.Parse(JsonConvert.SerializeObject(new UsersWechat()));
public WechatAuthProviderApi(ICacheManager cacheManager,IExternalAuthConfiguration externalAuthConfiguration)
{
_cacheManager = cacheManager;
_externalAuthConfiguration = externalAuthConfiguration;
var r = externalAuthConfiguration.Providers.First(p => p.Name == Name);
_options = new WeChatMiniProgramOptions
{
AppId = r.ClientId,
Secret = r.ClientSecret
}; }
public override async Task<ExternalAuthUserInfo> GetUserInfo(string accessCode)
{
UsersWechat wechat = new UsersWechat(); string accessToken = _cacheManager.GetCache("CacheName").Get("Login", () => GetToken(_options.AppId, _options.Secret));
if (!string.IsNullOrWhiteSpace(accessToken))
{
var url = string.Format(Config.ApiWorkHost + "/cgi-bin/user/getuserinfo?access_token={0}&code={1}", accessToken, accessCode); var redata = Get.GetJson<GetUserResult>(url);
if (!string.IsNullOrWhiteSpace(redata.user_ticket))
{
UserTicket tiket = new UserTicket
{
user_ticket = redata.user_ticket
};
url = string.Format(Config.ApiWorkHost + "/cgi-bin/user/getuserdetail?access_token={0}", accessToken);
// wechat = Post.GetResult<UsersWechat>(JsonConvert.SerializeObject(tiket));
wechat = await GetUserMsg(url, tiket);
}
} var t = wechat == null ? new ExternalAuthUserInfo() : new ExternalAuthUserInfo
{
EmailAddress = wechat.email,
Surname = wechat.name,
ProviderKey = wechat.userid,
Provider = Name,
Name = wechat.userid
};
return t; } private async Task<UsersWechat> GetUserMsg(string url, UserTicket tiket)
{
//序列化将要传输的对象
string obj = JsonConvert.SerializeObject(tiket);
HttpContent content = new StringContent(obj); HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var result = await client.PostAsync(url, content);
if (result.IsSuccessStatusCode)
{
string re = await result.Content.ReadAsStringAsync();
var jo = JObject.Parse(re);
if (jo.IsValid(schema))
{
var m = JsonConvert.DeserializeObject<UsersWechat>(re);
return m;
}
}
return null;
} private string GetToken(string AppId, string Secret)
{
if (string.IsNullOrWhiteSpace(AppId) || string.IsNullOrWhiteSpace(Secret))
{
return "";
}
return Senparc.Weixin.Work.Containers.AccessTokenContainer.TryGetTokenAsync(AppId, Secret).Result;
} public class UsersWechat
{
public string userid { get; set; }
public string name { get; set; } public string mobile { get; set; } public string gender { get; set; } public string email { get; set; } public string avatar { get; set; } public string qr_code { get; set; } } public class GetUserResult
{
public string errcode { get; set; }
public string errmsg { get; set; }
public string CorpId { get; set; }
public string UserId { get; set; }
public string DeviceId { get; set; } public string user_ticket { get; set; } public string expires_in { get; set; } } public class UserTicket
{
public string user_ticket { get; set; }
} }
}
以上就是企业微信登录的代码了。
最后,我们怎么开启企业微信的登录呢?
我们还需要再运行站点下的appsettings.json文件中加上我们企业微信的appid和密钥
"Facebook": {
"IsEnabled": "true",
"AppId": "",
"AppSecret": ""
},
"Google": {
"IsEnabled": "true",
"ClientId": "",
"ClientSecret": ""
},
"Wechat": {
"IsEnabled": "true",
"AppId": "xxxx",
"Secret": "xxx" },
最后一步:在站点的BanchWebHostModule.cs的ConfigureExternalAuthProviders方法下添加注册
if (bool.Parse(_appConfiguration["Authentication:Wechat:IsEnabled"]))
{
externalAuthConfiguration.Providers.Add(
new ExternalLoginProviderInfo(
WechatAuthProviderApi.Name,
_appConfiguration["Authentication:Wechat:AppId"],
_appConfiguration["Authentication:Wechat:Secret"],
typeof(QYWechatAuthProviderApi)
)
);
}
自此,企业微信登录就完成了,这里主要用到的是依赖注入和反射。







