using System.Security.Claims; using System.Text.Encodings.Web; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using static Yi.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants; namespace Yi.Framework.AspNetCore.Authentication.OAuth.Gitee { public class GiteeAuthenticationHandler : OauthAuthenticationHandler { public GiteeAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, IHttpClientFactory httpClientFactory) : base(options, logger, encoder, httpClientFactory) { } public override string AuthenticationSchemeNmae => GiteeAuthenticationDefaults.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("code",code) }; var tokenModel = await SendHttpRequestAsync(GiteeAuthenticationDefaults.TokenEndpoint, tokenQueryKv,HttpMethod.Post); //获取 userInfo var userInfoQueryKv = new List>() { new KeyValuePair("access_token",tokenModel.access_token), }; var userInfoMdoel = await SendHttpRequestAsync(GiteeAuthenticationDefaults.UserInformationEndpoint, userInfoQueryKv); List claims = new List() { new Claim(Claims.AvatarUrl, userInfoMdoel.avatar_url), new Claim(Claims.Url, userInfoMdoel.url), new Claim(AuthenticationConstants.OpenId,userInfoMdoel.id.ToString()), new Claim(AuthenticationConstants.Name, userInfoMdoel.name), new Claim(AuthenticationConstants.AccessToken, tokenModel.access_token) }; return claims; } } }