我想尽可能简单地编写自定义身份验证
[MyAuth]
public async Task<string> Index()
{
return "tested";
}
public class MyAuth : AuthorizeAttribute
{
public MyAuth()
{
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var context = context.HttpContext;
if (!something)
{
return Forbidden;
}
return Ok;
}
}
但是对于此代码,它希望我指定身份验证模式
InvalidOperationException:未指定authenticationScheme,也未找到DefaultChallengeScheme。
我该如何在其中注入数据库上下文?
[MyAuth]
public async Task<string> Index()
{
return "tested";
}
public class MyAuth : AuthorizeAttribute
{
private readonly DatabaseContext context;
public MyAuth(DatabaseContext db)
{
context = db;
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var context = context.HttpContext;
if (!something)
{
return Forbidden;
}
return Ok;
}
}
还是我实际上必须使用自己的Middleware
?但如果是,那么我如何确定请求是否到达要求[Authorize]
的端点?