Как использовать фильтры аутентификации в .NET CoreC#

Место общения программистов C#
Ответить
Anonymous
 Как использовать фильтры аутентификации в .NET Core

Сообщение Anonymous »


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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»