Search code examples
c#asp.net-mvc

Default Authentication scheme in ASP.NET MVC


This is the error message I'm getting:

System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultForbidScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).

The code I'm using in my controller:

if (!user.Email.Equals("my Dummy Address"))
{
    return Forbid();
}

I'm using the following code though...

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthentication(BearerTokenDefaults.AuthenticationScheme);

Why am I still getting the "No authenticationScheme was specified" error?


Solution

  • Found the solution...

    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddAuthentication().AddBearerToken(BearerTokenDefaults.AuthenticationScheme)
    ...
    app.UseAuthentication();
    app.UseAuthorization();