【发布时间】:2020-11-21 07:08:31
【问题描述】:
我们有一个与 ASP.NET Core API-Controller (V3.1) 配合使用的 Blazor WebAssembly-App。 Authentication Cookie 与我们的 Api-Controller 中的[Authorize] 属性配合得很好:
namespace BlazorWebAssemblyApp.Server.Controllers
{
[Route("[controller]")]
[ApiController]
public class ClientsController : ControllerBase
{
private const string AuthSchemes = CookieAuthenticationDefaults.AuthenticationScheme;
[HttpGet("search")]
[Authorize(AuthenticationSchemes = AuthSchemes)]
public ActionResult<List<Shared.Client>> Search(Shared.Client.SearchProperty pProperty, string pText)
{
// [...]
}
}
}
我们正在使用here 描述的解决方案来返回 HTTP 状态代码 403 Forbidden,而不是路由到登录页面。
namespace BlazorWebAssemblyApp.Server
{
public class Startup
{
// [...]
public void ConfigureServices(IServiceCollection services)
{
// [...]
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => {
options.Events.OnRedirectToAccessDenied = context => {
context.Response.StatusCode = 403;
return Task.CompletedTask;
};
});
}
}
}
但解决方案不起作用。应用仍然重定向到默认路径/Account/Login:
如何在 Blazor Web Assembly 应用程序中返回 http 状态代码 403 Forbidden 而不是重定向?
【问题讨论】:
-
“返回 403”究竟是什么意思,您希望用户看到什么?
-
@HenkHolterman:当我得到一个 http-status 代码 403 时,我可以通过一个合适的错误消息来处理它并将用户重定向到登录页面。
-
有没有机会看看你的 blazor web 客户端如何处理 cookie,我对 cookie 比 JWT 障碍更有趣,但我不知道如何在我的 webassambly http 客户端中保存 cookie跨度>
标签: asp.net asp.net-core asp.net-web-api blazor blazor-webassembly