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..832eeae 100644 --- a/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs +++ b/Yes.Infrastructure/Authorizations/AuthenticationExtensions.cs @@ -1,4 +1,6 @@ -namespace Yes.Infrastructure.Authorizations +using Microsoft.AspNetCore.Http.Extensions; + +namespace Yes.Infrastructure.Authorizations { public static class AuthenticationExtensions { @@ -16,10 +18,21 @@ options.Events.OnRedirectToAccessDenied = options.Events.OnRedirectToLogin = c => { - c.Response.StatusCode = StatusCodes.Status401Unauthorized; - return Task.FromResult(Result.Error("请登录后操作!")); + if(c.Request.GetDisplayUrl().Contains("/api")) + { + c.Response.ContentType = "application/json"; + c.Response.StatusCode = StatusCodes.Status401Unauthorized; + return Task.FromResult(Result.Error("请登录后操作!")); + } + else + { + c.Response.Redirect(c.RedirectUri); + return Task.CompletedTask; + } }; }); + + 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; + } + } +}