feat: 大抵完成初版框架

main
NoahLan 10 months ago
parent e6725d7938
commit ba1a0e86fd

@ -0,0 +1,9 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
public class AuthenticationConstants
{
public const string OpenId = "urn:openid";
public const string UnionId = "urn:unionid";
public const string AccessToken = "urn:access_token";
public const string Name = "urn:name";
}

@ -0,0 +1,20 @@
using Newtonsoft.Json;
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
public class AuthenticationErrCodeModel
{
[JsonProperty(PropertyName = "error")] public string Error { get; set; }
[JsonProperty(PropertyName = "error_description")]
public string ErrorDescription { get; set; }
public static void VerifyErrResponse(string content)
{
var model = JsonConvert.DeserializeObject<AuthenticationErrCodeModel>(content);
if (model.Error != null)
{
throw new Exception($"第三方授权返回错误 错误码 [{model.Error}] 错误详情 [{model.ErrorDescription}]");
}
}
}

@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Authentication.OAuth;
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
public class AuthenticationOAuthOptions : OAuthOptions
{
/// <summary>
/// 跳转地址
/// </summary>
public string RedirectUri { get; set; }
}

@ -1,5 +0,0 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
public class Class1
{
}

@ -0,0 +1,10 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public static class GiteeAuthenticationConstants
{
public static class Claims
{
public const string Url = "urn:gitee:url";
public const string AvatarUrl = "urn:gitee:avatarUrl";
}
}

@ -0,0 +1,44 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public static class GiteeAuthenticationDefaults
{
/// <summary>
/// Default value for <see cref="AuthenticationScheme.Name"/>.
/// </summary>
public const string AuthenticationScheme = "Gitee";
/// <summary>
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
/// </summary>
public static readonly string DisplayName = "Gitee";
/// <summary>
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
/// </summary>
public static readonly string Issuer = "Gitee";
/// <summary>
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
/// </summary>
public static readonly string CallbackPath = "/signin-gitee";
/// <summary>
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
/// </summary>
public static readonly string AuthorizationEndpoint = "https://gitee.com/oauth/authorize";
/// <summary>
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
/// </summary>
public static readonly string TokenEndpoint = "https://gitee.com/oauth/token";
/// <summary>
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
/// </summary>
public static readonly string UserInformationEndpoint = "https://gitee.com/api/v5/user";
/// <summary>
/// Default value for <see cref="GiteeAuthenticationOptions.UserEmailsEndpoint"/>.
/// </summary>
public static readonly string UserEmailsEndpoint = "https://gitee.com/api/v5/emails";
}

@ -0,0 +1,66 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public static class GiteeAuthenticationExtensions
{
/// <summary>
/// Adds <see cref="GiteeAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Gitee authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitee([NotNull] this AuthenticationBuilder builder)
{
return builder.AddGitee(GiteeAuthenticationDefaults.AuthenticationScheme, options => { });
}
/// <summary>
/// Adds <see cref="GiteeAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Gitee authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitee(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<GiteeAuthenticationOptions> configuration)
{
return builder.AddGitee(GiteeAuthenticationDefaults.AuthenticationScheme, configuration);
}
/// <summary>
/// Adds <see cref="GiteeAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Gitee authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Gitee options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitee(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[NotNull] Action<GiteeAuthenticationOptions> configuration)
{
return builder.AddGitee(scheme, GiteeAuthenticationDefaults.DisplayName, configuration);
}
/// <summary>
/// Adds <see cref="GiteeAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Gitee authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="caption">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Gitee options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddGitee(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[CanBeNull] string caption,
[NotNull] Action<GiteeAuthenticationOptions> configuration)
{
return builder.AddScheme<GiteeAuthenticationOptions, GiteeAuthenticationHandler>(scheme, caption, configuration);
}
}

@ -0,0 +1,54 @@
using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using static NPin.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants;
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public class GiteeAuthenticationHandler : OAuthAuthenticationHandler<GiteeAuthenticationOptions>
{
public GiteeAuthenticationHandler(IOptionsMonitor<GiteeAuthenticationOptions> options, ILoggerFactory logger,
UrlEncoder encoder, IHttpClientFactory httpClientFactory) : base(options, logger, encoder, httpClientFactory)
{
}
public override string AuthenticationSchemeName => GiteeAuthenticationDefaults.AuthenticationScheme;
protected override async Task<List<Claim>> GetAuthTicketAsync(string code)
{
// 获取 accessToken
var tokenQueryKv = new List<KeyValuePair<string, string?>>
{
new("grant_type", "authorization_code"),
new("client_id", Options.ClientId),
new("client_secret", Options.ClientSecret),
new("redirect_uri", Options.RedirectUri),
new("code", code)
};
var tokenModel =
await SendHttpRequestAsync<GiteeAuthticationcationTokenResponse>(GiteeAuthenticationDefaults.TokenEndpoint,
tokenQueryKv, HttpMethod.Post);
// 获取 userInfo
var userInfoQueryKv = new List<KeyValuePair<string, string?>>
{
new("access_token", tokenModel.access_token),
};
var userInfoModel =
await SendHttpRequestAsync<GiteeAuthticationcationUserInfoResponse>(
GiteeAuthenticationDefaults.UserInformationEndpoint, userInfoQueryKv);
List<Claim> claims =
[
new(Claims.AvatarUrl, userInfoModel.avatar_url),
new(Claims.Url, userInfoModel.url),
new(AuthenticationConstants.OpenId, userInfoModel.id.ToString()),
new(AuthenticationConstants.Name, userInfoModel.name),
new(AuthenticationConstants.AccessToken, tokenModel.access_token)
];
return claims;
}
}

@ -0,0 +1,36 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using static NPin.Framework.AspNetCore.Authentication.OAuth.Gitee.GiteeAuthenticationConstants;
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public class GiteeAuthenticationOptions: AuthenticationOAuthOptions
{
public GiteeAuthenticationOptions()
{
ClaimsIssuer = GiteeAuthenticationDefaults.Issuer;
CallbackPath = GiteeAuthenticationDefaults.CallbackPath;
AuthorizationEndpoint = GiteeAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = GiteeAuthenticationDefaults.TokenEndpoint;
UserInformationEndpoint = GiteeAuthenticationDefaults.UserInformationEndpoint;
UserEmailsEndpoint = GiteeAuthenticationDefaults.UserEmailsEndpoint;
Scope.Add("user_info");
Scope.Add("emails");
ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
ClaimActions.MapJsonKey(ClaimTypes.Name, "login");
ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
ClaimActions.MapJsonKey(Claims.Url, "url");
}
/// <summary>
/// Gets or sets the address of the endpoint exposing
/// the email addresses associated with the logged in user.
/// </summary>
public string UserEmailsEndpoint { get; set; }
}

@ -0,0 +1,55 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.Gitee;
public class GiteeAuthticationcationTokenResponse
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string refresh_token { get; set; }
public string scope { get; set; }
public long created_at { get; set; }
}
public class GiteeAuthticationcationOpenIdResponse
{
public string client_id { get; set; }
public string openid { get; set; }
}
public class GiteeAuthticationcationUserInfoResponse
{
/// <summary>
/// 也可以等于openId
/// </summary>
public int id { get; set; }
public string login { get; set; }
public string name { get; set; }
public string avatar_url { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string remark { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public string blog { get; set; }
public string weibo { get; set; }
public string bio { get; set; }
public int public_repos { get; set; }
public int public_gists { get; set; }
public int followers { get; set; }
public int following { get; set; }
public int stared { get; set; }
public int watched { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public string email { get; set; }
}

@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
/// <summary>
/// AspNet.Security.OAuth.QQ
/// </summary>
[DependsOn(typeof(NPinFrameworkAspNetCoreModule))]
public class NPinFrameworkAspNetCoreAuthenticationOAuthModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddHttpClient();
}
}

@ -0,0 +1,108 @@
using System.Security.Claims;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace NPin.Framework.AspNetCore.Authentication.OAuth;
public abstract class OAuthAuthenticationHandler<TOptions> : AuthenticationHandler<TOptions>
where TOptions : AuthenticationSchemeOptions, new()
{
public abstract string AuthenticationSchemeName { get; }
// private AuthenticationScheme _scheme;
protected IHttpClientFactory HttpClientFactory { get; }
protected HttpClient HttpClient { get; }
public OAuthAuthenticationHandler(IOptionsMonitor<TOptions> options, ILoggerFactory logger, UrlEncoder encoder,
IHttpClientFactory httpClientFactory) :
base(options, logger, encoder)
{
HttpClientFactory = httpClientFactory;
HttpClient = httpClientFactory.CreateClient();
}
/// <summary>
/// Claims 转换为 票据
/// </summary>
/// <param name="claims"></param>
/// <returns></returns>
private AuthenticationTicket ConvertTicket(List<Claim> claims)
{
var claimsIdentity = new ClaimsIdentity(claims.ToArray(), AuthenticationSchemeName);
var principal = new ClaimsPrincipal(claimsIdentity);
return new AuthenticationTicket(principal, AuthenticationSchemeName);
}
protected async Task<THttpModel> SendHttpRequestAsync<THttpModel>(string url,
IEnumerable<KeyValuePair<string, string?>> query,
HttpMethod? httpMethod = null)
{
httpMethod ??= HttpMethod.Get;
var queryUrl = QueryHelpers.AddQueryString(url, query);
HttpResponseMessage response = null;
if (httpMethod == HttpMethod.Get)
{
response = await HttpClient.GetAsync(queryUrl);
}
else if (httpMethod == HttpMethod.Post)
{
response = await HttpClient.PostAsync(queryUrl, null);
}
if (response == null)
{
throw new NotSupportedException($"不支持的请求方式: {httpMethod}");
}
var content = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception($"授权服务器请求错误,请求地址[{queryUrl}] 错误信息[{content}]");
}
VerifyErrResponse(content);
var model = JsonConvert.DeserializeObject<THttpModel>(content);
return model!;
}
protected virtual void VerifyErrResponse(string content)
{
AuthenticationErrCodeModel.VerifyErrResponse(content);
}
/// <summary>
/// 获取登录票据
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
protected abstract Task<List<Claim>> GetAuthTicketAsync(string code);
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Context.Request.Query.ContainsKey("code"))
{
return AuthenticateResult.Fail("回调中未包含code参数");
}
var code = Context.Request.Query["code"].ToString();
List<Claim> authTicket;
try
{
authTicket = await GetAuthTicketAsync(code);
}
catch (Exception ex)
{
return AuthenticateResult.Fail(ex.Message ?? "未知错误");
}
// 获取登录票据成功
return AuthenticateResult.Success(ConvertTicket(authTicket));
}
}

@ -0,0 +1,17 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.QQ;
/// <summary>
/// QQ登录的常量
/// </summary>
public static class QQAuthenticationConstants
{
public static class Claims
{
public const string AvatarFullUrl = "urn:qq:avatar_full";
public const string AvatarUrl = "urn:qq:avatar";
public const string PictureFullUrl = "urn:qq:picture_full";
public const string PictureMediumUrl = "urn:qq:picture_medium";
public const string PictureUrl = "urn:qq:picture";
public const string UnionId = "urn:qq:unionid";
}
}

@ -0,0 +1,47 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.QQ;
/// <summary>
/// Default values for QQ authentication.
/// </summary>
public class QQAuthenticationDefaults
{
/// <summary>
/// Default value for <see cref="AuthenticationScheme.Name"/>.
/// </summary>
public const string AuthenticationScheme = "QQ";
/// <summary>
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
/// </summary>
public static readonly string DisplayName = "QQ";
/// <summary>
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
/// </summary>
public static readonly string Issuer = "QQ";
/// <summary>
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
/// </summary>
public static readonly string CallbackPath = "/signin-qq";
/// <summary>
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
/// </summary>
public static readonly string AuthorizationEndpoint = "https://graph.qq.com/oauth2.0/authorize";
/// <summary>
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
/// </summary>
public static readonly string TokenEndpoint = "https://graph.qq.com/oauth2.0/token";
/// <summary>
/// Default value for <see cref="QQAuthenticationOptions.UserIdentificationEndpoint"/>.
/// </summary>
public static readonly string UserIdentificationEndpoint = "https://graph.qq.com/oauth2.0/me";
/// <summary>
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
/// </summary>
public static readonly string UserInformationEndpoint = "https://graph.qq.com/user/get_user_info";
}

@ -0,0 +1,67 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;
namespace NPin.Framework.AspNetCore.Authentication.OAuth.QQ;
public static class QQAuthenticationExtensions
{
/// <summary>
/// Adds <see cref="QQAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables QQ authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddQQ([NotNull] this AuthenticationBuilder builder)
{
return builder.AddQQ(QQAuthenticationDefaults.AuthenticationScheme, options => { });
}
/// <summary>
/// Adds <see cref="QQAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables QQ authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddQQ(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<QQAuthenticationOptions> configuration)
{
return builder.AddQQ(QQAuthenticationDefaults.AuthenticationScheme, configuration);
}
/// <summary>
/// Adds <see cref="QQAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables QQ authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the QQ options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddQQ(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[NotNull] Action<QQAuthenticationOptions> configuration)
{
return builder.AddQQ(scheme, QQAuthenticationDefaults.DisplayName, configuration);
}
/// <summary>
/// Adds <see cref="QQAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables QQ authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="caption">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the QQ options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddQQ(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme,
[CanBeNull] string caption,
[NotNull] Action<QQAuthenticationOptions> configuration)
{
return builder.AddScheme<QQAuthenticationOptions, QQAuthenticationHandler>(scheme, caption, configuration);
}
}

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

@ -0,0 +1,40 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using static NPin.Framework.AspNetCore.Authentication.OAuth.QQ.QQAuthenticationConstants;
namespace NPin.Framework.AspNetCore.Authentication.OAuth.QQ;
public class QQAuthenticationOptions: AuthenticationOAuthOptions
{
public QQAuthenticationOptions()
{
ClaimsIssuer = QQAuthenticationDefaults.Issuer;
CallbackPath = QQAuthenticationDefaults.CallbackPath;
AuthorizationEndpoint = QQAuthenticationDefaults.AuthorizationEndpoint;
TokenEndpoint = QQAuthenticationDefaults.TokenEndpoint;
UserIdentificationEndpoint = QQAuthenticationDefaults.UserIdentificationEndpoint;
UserInformationEndpoint = QQAuthenticationDefaults.UserInformationEndpoint;
Scope.Add("get_user_info");
ClaimActions.MapJsonKey(ClaimTypes.Name, "nickname");
ClaimActions.MapJsonKey(ClaimTypes.Gender, "gender");
ClaimActions.MapJsonKey(Claims.PictureUrl, "figureurl");
ClaimActions.MapJsonKey(Claims.PictureMediumUrl, "figureurl_1");
ClaimActions.MapJsonKey(Claims.PictureFullUrl, "figureurl_2");
ClaimActions.MapJsonKey(Claims.AvatarUrl, "figureurl_qq_1");
ClaimActions.MapJsonKey(Claims.AvatarFullUrl, "figureurl_qq_2");
}
/// <summary>
/// Gets or sets if the union Id (the primary key of an owner for different apps of the QQ platform) should be put into the user claims.
/// </summary>
public bool ApplyForUnionId { get; set; }
/// <summary>
/// Gets or sets the URL of the user identification endpoint (a.k.a. the "OpenID endpoint").
/// </summary>
public string UserIdentificationEndpoint { get; set; }
}

@ -0,0 +1,78 @@
namespace NPin.Framework.AspNetCore.Authentication.OAuth.QQ;
public class QQAuthticationcationTokenResponse
{
public string access_token { get; set; }
public string expires_in { get; set; }
public string refresh_token { get; set; }
public string openid { get; set; }
}
public class QQAuthticationcationOpenIdResponse
{
public string client_id { get; set; }
public string openid { get; set; }
}
public class QQAuthticationcationUserInfoResponse
{
// 返回码
public int ret { get; set; }
// 如果ret<0会有相应的错误信息提示
// 返回数据全部用UTF-8编码
public string msg { get; set; }
// 判断是否有数据丢失
// 0或者不返回没有数据丢失可以缓存
// 1有部分数据丢失或错误不要缓存
public int is_lost { get; set; }
// 用户在QQ空间的昵称
public string nickname { get; set; }
// 大小为30x30像素的QQ空间头像URL
public string figureurl { get; set; }
// 大小为50x50像素的QQ空间头像URL
public string figureurl_1 { get; set; }
// 大小为100x100像素的QQ空间头像URL
public string figureurl_2 { get; set; }
// 大小为40x40像素的QQ头像URL
public string figureurl_qq_1 { get; set; }
// 大小为100x100像素的QQ头像URL
// 需要注意不是所有的用户都拥有QQ的100x100的头像但40x40像素则是一定会有
public string figureurl_qq_2 { get; set; }
// 性别。如果获取不到则默认返回"男"
public string gender { get; set; }
// 性别类型。默认返回2
public int gender_type { get; set; }
// 省
public string province { get; set; }
// 市
public string city { get; set; }
// 年
public int year { get; set; }
// 星座
public string constellation { get; set; }
// 标识用户是否为黄钻用户
public int is_yellow_vip { get; set; }
// 黄钻等级
public int yellow_vip_level { get; set; }
// 是否为年费黄钻用户
public int is_yellow_year_vip { get; set; }
}

@ -1,5 +0,0 @@
namespace NPin.Framework.Ddd.Application.Contracts;
public class Class1
{
}

@ -0,0 +1,9 @@
using Volo.Abp;
using Volo.Abp.Application.Services;
namespace NPin.Framework.Ddd.Application.Contracts;
public interface IDeleteAppService<in TKey> : Volo.Abp.Application.Services.IDeleteAppService<TKey>
{
Task DeleteAsync(IEnumerable<TKey> ids);
}

@ -0,0 +1,30 @@
using Volo.Abp.Application.Services;
namespace NPin.Framework.Ddd.Application.Contracts;
public interface INPinCrudAppService<TEntityDto, in TKey> : ICrudAppService<TEntityDto, TKey>
{
}
public interface
INPinCrudAppService<TEntityDto, in TKey, in TGetListInput> : ICrudAppService<TEntityDto, TKey, TGetListInput>
{
}
public interface
INPinCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput> : ICrudAppService<TEntityDto, TKey,
TGetListInput, TCreateInput>
{
}
public interface INPinCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput> :
ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
{
}
public interface INPinCrudAppService<TEntityDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput,
in TUpdateInput> :
ICrudAppService<TEntityDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>,
IDeleteAppService<TKey>
{
}

@ -0,0 +1,9 @@
using Volo.Abp.Application.Dtos;
namespace NPin.Framework.Ddd.Application.Contracts;
public interface IPageTimeResultRequestDto : IPagedAndSortedResultRequest
{
DateTime? StartTime { get; set; }
DateTime? EndTime { get; set; }
}

@ -0,0 +1,7 @@
using Volo.Abp.Application.Dtos;
namespace NPin.Framework.Ddd.Application.Contracts;
public interface IPagedAllResultRequestDto : IPageTimeResultRequestDto, IPagedAndSortedResultRequest
{
}

@ -0,0 +1,10 @@
using Volo.Abp.Application;
using Volo.Abp.Modularity;
namespace NPin.Framework.Ddd.Application.Contracts;
[DependsOn(typeof(AbpDddApplicationContractsModule))]
public class NPinFrameworkDddApplicationContractsModule: AbpModule
{
}

@ -0,0 +1,16 @@
using Volo.Abp.Application.Dtos;
namespace NPin.Framework.Ddd.Application.Contracts;
public class PagedAllResultRequestDto : PagedAndSortedResultRequestDto, IPagedAllResultRequestDto
{
/// <summary>
/// 查询开始时间条件
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
/// 查询结束时间条件
/// </summary>
public DateTime? EndTime { get; set; }
}

@ -1,5 +0,0 @@
namespace NPin.Framework.Ddd.Application;
public class Class1
{
}

@ -0,0 +1,148 @@
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
namespace NPin.Framework.Ddd.Application;
public abstract class
NPinCrudAppService<TEntity, TEntityDto, TKey> : NPinCrudAppService<TEntity, TEntityDto, TKey,
PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected NPinCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class NPinCrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
: NPinCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected NPinCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class NPinCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
: NPinCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected NPinCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class NPinCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: NPinCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected NPinCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
}
public abstract class NPinCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput,
TUpdateInput> :
CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
protected NPinCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{
}
/// <summary>
/// 多查/批量查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
List<TEntity> entities;
// 区分查/批量查
if (input is IPagedResultRequest pagedInput)
{
entities = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount,
string.Empty);
}
else
{
entities = await Repository.GetListAsync();
}
var total = await Repository.GetCountAsync();
var output = await MapToGetListOutputDtosAsync(entities);
return new PagedResultDto<TGetListOutputDto>(total, output);
}
/// <summary>
/// 多删
/// </summary>
/// <param name="ids"></param>
[RemoteService(isEnabled: true)]
public virtual async Task DeleteAsync(IEnumerable<TKey> ids)
{
await Repository.DeleteManyAsync(ids);
}
/// <summary>
/// 单删(覆盖)
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[RemoteService(isEnabled: false)]
public override Task DeleteAsync(TKey id)
{
return base.DeleteAsync(id);
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<IActionResult> GetExportExcelAsync(TGetListInput input)
{
if (input is IPagedResultRequest paged)
{
// 不进行分页
paged.SkipCount = 0;
paged.MaxResultCount = LimitedResultRequestDto.MaxMaxResultCount;
}
var output = await this.GetListAsync(input);
var dirPath = $"/temp";
var filename = $"{typeof(TEntity).Name}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{Guid.NewGuid()}";
var filePath = $"{dirPath}/{filename}.xlsx";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
await MiniExcel.SaveAsAsync(filePath, output.Items);
return new PhysicalFileResult(filePath, "application/vnd.ms-excel");
}
/// <summary>
/// 导入Excel
/// </summary>
/// <param name="input"></param>
public virtual async Task PostImportExcelAsync(List<TCreateInput> input)
{
// var entities = input.Select(MapToEntity).ToList();
// await Repository.InsertManyAsync(entities);
// 需子类自行实现
throw new NotImplementedException();
}
}

@ -0,0 +1,18 @@
using NPin.Framework.Ddd.Application.Contracts;
using Volo.Abp;
using Volo.Abp.Application;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Modularity;
namespace NPin.Framework.Ddd.Application;
[DependsOn(typeof(AbpDddApplicationModule), typeof(NPinFrameworkDddApplicationContractsModule))]
public class NPinFrameworkDddApplicationModule : AbpModule
{
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
// 分页限制调整
LimitedResultRequestDto.DefaultMaxResultCount = 10;
LimitedResultRequestDto.MaxMaxResultCount = 10000;
}
}

@ -202,8 +202,21 @@ public class SqlSugarDbContext : ISqlSugarDbContext
{ {
} }
/// <summary>
/// 实体配置
/// </summary>
/// <param name="property"></param>
/// <param name="column"></param>
protected virtual void EntityService(PropertyInfo property, EntityColumnInfo column) protected virtual void EntityService(PropertyInfo property, EntityColumnInfo column)
{ {
if (property.PropertyType == typeof(ExtraPropertyDictionary))
{
column.IsIgnore = true;
}
if (property.Name == nameof(Entity<object>.Id))
{
column.IsPrimarykey = true;
}
} }
public void Backup() public void Backup()

Loading…
Cancel
Save