gpt4 book ai didi

c# - 如何使用 BasicAuthentication (c# dotnet core) 将用户重定向到登录页面而不是 401 错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:03 24 4
gpt4 key购买 nike

好吧,我正在使用 netcoreapp2.2 ,并且我已将我的项目配置为使用“BasicAuthentication”,一切似乎都很好。

            services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication",null);

但是发生的事情是,无论哪里有 [Authorize],都会使页面返回 401 错误(我看到这对于 BasicAuthentication 来说是正常的),但是如何让它重定向到我的登录 View ?而不是 401?

谢谢

最佳答案

您可以在服务上使用 ConfigureApplicationCookie 扩展来指定登录页面路径。当您收到 401 时,它将自动重定向到那里。

services.ConfigureApplicationCookie(options => options.LoginPath = "pathToLoginPage");

关于c# - 如何使用 BasicAuthentication (c# dotnet core) 将用户重定向到登录页面而不是 401 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56279249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com