之后是,该在制作的APICI中设置认证过滤器的指南。 这完全容易,除非在测试时,我获得这一例外。
No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).
每当我试图打一个终点。
I ve look at this Guide,其中还规定了authenticationScheme
。 like
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme,
options => builder.Configuration.Bind("JwtSettings", options))
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options => builder.Configuration.Bind("CookieSettings", options));
但我不想这样做。 是否有办法通过属性使用认证过滤器?
在这方面,我是如何安排的。
基本过滤器:
[AttributeUsage(AttributeTargets.All)]
public class BasicFilter : AuthorizeAttribute, IAuthenticationFilter
{
async public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
BaseRequest? request = await AuthenticationHelper.RequestIsValid<BaseRequest>(context);
if (request == null)
{
context.ErrorResult = new AuthenticationFailureResult(context.Request);
return;
}
GenericPrincipal principal = new GenericPrincipal(new GenericIdentity("User"), null);
context.Principal = principal;
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
var challenge = new AuthenticationHeaderValue("None");
context.Result = new ChallengeUnauthorizedResult(challenge, context.Result);
return Task.FromResult(0);
}
}
而随后又扔下了<条码>[巴塞尔代码]代码>和<条码>[阿法明]条码>等终点:
[Authorize]
[BasicFilter]
[HttpPost]
async public Task<ActionResult> CreateAccount()
{
}