Share via

REST API Server error using OAuth2 and Microsoft.AspNetCore.Identity

frankborty 41 Reputation points
2024-08-22T07:43:56.17+00:00

I have a simple REST API server with authentication based on OAuth2. Everything works as expected and I’m able to call the controllers by setting the correct bearer token. My goal is to manage authorization entirely and to do this, I wanted to use the Microsoft.AspNetCore.Identity class. However, as soon as I add the related configuration to my program.cs, the calls to the controller fail.

This is my program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(builder.Configuration);


builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();

This is my simple controller class:

[Route("api/[controller]")]
[ApiController]
public class OAuthTestController : ControllerBase
{
    [HttpGet("public")]
    public IActionResult GetPublicData()
    {
        return Ok("This is public data.");
    }

    [HttpGet("protected")]
    [Authorize]
    public IActionResult GetProtectedData()
    {
        return Ok("This is protected data, you are authenticated.");
    }
}

If I try to call the GetProtectedData() endpoint using PostMan, the request is redirected to a login page that does not exist, and therefore I receive a 404 error. Is it possible to use OAuth2 and Microsoft.AspNetCore.Identity together? Is it better to manually create the tables to save users and roles?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

Answer accepted by question author

Anonymous
2024-08-22T09:35:44.02+00:00

Hi @frankborty,

Try to replace AddIdentity with AddIdentityCore. Here is the sample code for you.

builder.Services.AddIdentityCore<IdentityUser>(options => { }) 
.AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,

Jason

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.