【发布时间】:2022-01-02 07:07:46
【问题描述】:
我有以下代码可以绕过在本地开发期间添加身份验证,我正在使用 Azure AD 和 .NET Core。
#if !DEBUG
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));
#endif
但是由于我的控制器受 Authorize 属性保护,我如何在本地开发期间绕过 Controller 内的 Authorize 属性:
[Authorize(Roles = "Buyer")]
public class ProductController : ApiBaseController
{
}
在 .NET Framework 中,我有以下代码来覆盖 Authorize 属性:
public class MyAuthorizeAttribute : AuthorizeAttribute
{
#if DEBUG
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return true;
}
#endif
}
.NET Core 的等效代码是什么?还是有其他方法可以覆盖 Startup.cs 类中的 Authorize 属性?
【问题讨论】:
-
在启动代码中放置一个标志,如果处于开发模式,则不应用身份验证管道。
标签: c# asp.net-core .net-core asp.net-core-webapi asp.net-core-3.1