diff --git a/src/YhCore.WebApi/Middleware/SsoProtectionMiddleware.cs b/src/YhCore.WebApi/Middleware/SsoProtectionMiddleware.cs index e49afe3..dc4878d 100644 --- a/src/YhCore.WebApi/Middleware/SsoProtectionMiddleware.cs +++ b/src/YhCore.WebApi/Middleware/SsoProtectionMiddleware.cs @@ -1,8 +1,9 @@ -using System; -using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; +using System; +using System.Threading.Tasks; +using Yh.Platform.Abstractions; using Yh.Platform.Auth; namespace YhCore.WebApi.Middleware @@ -93,10 +94,9 @@ namespace YhCore.WebApi.Middleware // 前端据此 401 做整页跳转登录(location.href = "/")。文档导航(浏览器地址栏 / Accept:text/html)仍走 302。 if (IsProtectedPath(context.Request.Path.Value, sso) && IsAjaxRequest(context.Request)) { - context.Response.StatusCode = StatusCodes.Status401Unauthorized; + context.Response.StatusCode = StatusCodes.Status200OK; context.Response.ContentType = "application/json; charset=utf-8"; - await context.Response.WriteAsync("{\"code\":4,\"msg\":\"未登录\",\"data\":null}"); - return; + throw new DException("未登录!"); } // 未认证 → Challenge → 302 重定向 CAS 登录页(ticket 校验与回跳由新框架 CasAuthenticationHandler 处理) diff --git a/src/YhCore.WebApi/Startup.cs b/src/YhCore.WebApi/Startup.cs index 4a741fa..11d8f7b 100644 --- a/src/YhCore.WebApi/Startup.cs +++ b/src/YhCore.WebApi/Startup.cs @@ -238,9 +238,7 @@ namespace YhCore.WebApi var user = context.User; if (user?.Identity?.IsAuthenticated != true) { - context.Response.StatusCode = 401; - await context.Response.WriteAsync("未登录(SSO 未开启或 Cookie 失效)"); - return; + throw new DException("未登录(SSO 未开启或 Cookie 失效)"); } var name = user.Identity?.Name ?? "-"; var id = user.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value ?? "-";