Как использовать фильтры аутентификации в .NET Core ⇐ C#
-
Anonymous
Как использовать фильтры аутентификации в .NET Core
I followed this guide to setup authentication filters in an API I created. It was all pretty easy and went well except now when testing, I get this exception
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).
whenever I try to hit an endpoint.
I've looked at this guide as well, where it does set the authenticationScheme like so
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => builder.Configuration.Bind("JwtSettings", options)) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => builder.Configuration.Bind("CookieSettings", options)); but I don't want any of these. Is there a way to just use authentication filter's via attribute syntax?
Here is how I have it setup.
Basic filter:
[AttributeUsage(AttributeTargets.All)] public class BasicFilter : AuthorizeAttribute, IAuthenticationFilter { async public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) { BaseRequest? request = await AuthenticationHelper.RequestIsValid(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); } } And then just throw a [BasicFilter] and [Authorize] attribute on an endpoint like so:
[Authorize] [BasicFilter] [HttpPost] async public Task CreateAccount() { }
Источник: https://stackoverflow.com/questions/781 ... n-net-core
I followed this guide to setup authentication filters in an API I created. It was all pretty easy and went well except now when testing, I get this exception
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).
whenever I try to hit an endpoint.
I've looked at this guide as well, where it does set the authenticationScheme like so
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => builder.Configuration.Bind("JwtSettings", options)) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => builder.Configuration.Bind("CookieSettings", options)); but I don't want any of these. Is there a way to just use authentication filter's via attribute syntax?
Here is how I have it setup.
Basic filter:
[AttributeUsage(AttributeTargets.All)] public class BasicFilter : AuthorizeAttribute, IAuthenticationFilter { async public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken) { BaseRequest? request = await AuthenticationHelper.RequestIsValid(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); } } And then just throw a [BasicFilter] and [Authorize] attribute on an endpoint like so:
[Authorize] [BasicFilter] [HttpPost] async public Task CreateAccount() { }
Источник: https://stackoverflow.com/questions/781 ... n-net-core
Мобильная версия