using System.Security.Claims; using System.Text.Encodings.Web; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using static Yi.Framework.AspNetCore.Authentication.OAuth.QQ.QQAuthenticationConstants; namespace Yi.Framework.AspNetCore.Authentication.OAuth.QQ { public class QQAuthenticationHandler : OauthAuthenticationHandler { public QQAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, IHttpClientFactory httpClientFactory) : base(options, logger, encoder, httpClientFactory) { } public override string AuthenticationSchemeNmae => QQAuthenticationDefaults.AuthenticationScheme; protected override async Task> GetAuthTicketAsync(string code) { //获取 accessToken var tokenQueryKv = new List>() { new KeyValuePair("grant_type","authorization_code"), new KeyValuePair("client_id",Options.ClientId), new KeyValuePair("client_secret",Options.ClientSecret), new KeyValuePair("redirect_uri",Options.RedirectUri), new KeyValuePair("fmt","json"), new KeyValuePair("need_openid","1"), new KeyValuePair("code",code) }; var tokenModel = await SendHttpRequestAsync(QQAuthenticationDefaults.TokenEndpoint, tokenQueryKv); //获取 userInfo var userInfoQueryKv = new List>() { new KeyValuePair("access_token",tokenModel.access_token), new KeyValuePair("oauth_consumer_key",Options.ClientId), new KeyValuePair("openid",tokenModel.openid), }; var userInfoMdoel = await SendHttpRequestAsync(QQAuthenticationDefaults.UserInformationEndpoint, userInfoQueryKv); List claims = new List() { new Claim(Claims.AvatarFullUrl, userInfoMdoel.figureurl_qq_2), new Claim(Claims.AvatarUrl, userInfoMdoel.figureurl_qq_1), new Claim(Claims.PictureFullUrl, userInfoMdoel.figureurl_2), new Claim(Claims.PictureMediumUrl, userInfoMdoel.figureurl_qq_1), new Claim(Claims.PictureUrl, userInfoMdoel.figureurl), new Claim(AuthenticationConstants.OpenId, tokenModel.openid), new Claim(AuthenticationConstants.Name, userInfoMdoel.nickname), new Claim(AuthenticationConstants.AccessToken, tokenModel.access_token), }; return claims; } } }