From dd86a5a81893a4982292276a0ad77e777f16fedb Mon Sep 17 00:00:00 2001 From: xiang <27800734@qq.com> Date: Wed, 7 May 2025 00:10:03 +0800 Subject: [PATCH] 1 --- Yes.Blog/DependencyInjection.cs | 4 ++-- Yes.Blog/Endpoints/Blogs/InstallEndpoint.cs | 2 +- .../Authorizations/AuthenticationExtensions.cs | 14 ++++++++++++-- .../Authorizations/AuthenticationScheme.cs | 2 ++ .../User/UserApiAuthorizeAttribute.cs | 12 ++++++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 Yes.Infrastructure/Authorizations/User/UserApiAuthorizeAttribute.cs diff --git a/Yes.Blog/DependencyInjection.cs b/Yes.Blog/DependencyInjection.cs index bdaba09..5e796f0 100644 --- a/Yes.Blog/DependencyInjection.cs +++ b/Yes.Blog/DependencyInjection.cs @@ -30,9 +30,9 @@ namespace Yes.Blog services.AddAuthorization(); - builder.Services.AddEndpointsApiExplorer(); + services.AddEndpointsApiExplorer(); - builder.Services.AddOpenApi(options => + services.AddOpenApi(options => { options.AddDocumentTransformer((document, context, cancellationToken) => { diff --git a/Yes.Blog/Endpoints/Blogs/InstallEndpoint.cs b/Yes.Blog/Endpoints/Blogs/InstallEndpoint.cs index f311eef..11d9226 100644 --- a/Yes.Blog/Endpoints/Blogs/InstallEndpoint.cs +++ b/Yes.Blog/Endpoints/Blogs/InstallEndpoint.cs @@ -1,4 +1,4 @@ -using Yes.Blog.Endpoints.Admins; + namespace Yes.Blog.Endpoints.Blogs { diff --git a/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs b/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs index 8931aa9..635d5e6 100644 --- a/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs +++ b/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs @@ -5,6 +5,16 @@ public static IServiceCollection AddUserAuthentication(this IServiceCollection services) { services.AddAuthentication(AuthenticationScheme.UserScheme) + .AddCookie(AuthenticationScheme.UserScheme, options => + { + options.Cookie.Name = AuthenticationScheme.UserScheme; + options.LoginPath = "/admin/login"; + options.LogoutPath = "/admin/logout"; + options.ExpireTimeSpan = TimeSpan.FromDays(3); + options.SlidingExpiration = true; + }); + + services.AddAuthentication(AuthenticationScheme.UserApiScheme) .AddCookie(AuthenticationScheme.UserScheme, options => { options.Cookie.Name = AuthenticationScheme.UserScheme; @@ -16,8 +26,8 @@ options.Events.OnRedirectToAccessDenied = options.Events.OnRedirectToLogin = c => { - c.Response.StatusCode = StatusCodes.Status401Unauthorized; - return Task.FromResult(Result.Error("请登录后操作!")); + c.Response.StatusCode = StatusCodes.Status401Unauthorized; + return Task.FromResult(Result.Error("请登录后操作!")); }; }); return services; diff --git a/Yes.Infrastructure/Authorizations/AuthenticationScheme.cs b/Yes.Infrastructure/Authorizations/AuthenticationScheme.cs index 89df91a..71f92f2 100644 --- a/Yes.Infrastructure/Authorizations/AuthenticationScheme.cs +++ b/Yes.Infrastructure/Authorizations/AuthenticationScheme.cs @@ -5,5 +5,7 @@ public const string MerchanScheme = "merchan"; public const string UserScheme = "user"; + + public const string UserApiScheme = "userapi"; } } diff --git a/Yes.Infrastructure/Authorizations/User/UserApiAuthorizeAttribute.cs b/Yes.Infrastructure/Authorizations/User/UserApiAuthorizeAttribute.cs new file mode 100644 index 0000000..869734e --- /dev/null +++ b/Yes.Infrastructure/Authorizations/User/UserApiAuthorizeAttribute.cs @@ -0,0 +1,12 @@ +namespace Yes.Infrastructure.Authorizations.User +{ + + + public class UserApiAuthorizeAttribute : AuthorizeAttribute + { + public UserApiAuthorizeAttribute() + { + AuthenticationSchemes = AuthenticationScheme.UserApiScheme; + } + } +}