How to set up [Authorize] attribute with Aspnet.core.Components.Authorization?

sblb 1,231 Reputation points
2023-06-17T07:22:04.1066667+00:00

Hi, I put the Login/logout function in my application with Aspnet.core.Components.Authorization which means that when the person creates an account they can have access to the whole application.

I would like to introduce the [Authorize] attribute in specific controller.

I follow-up this link but it doesn't work https://learn.microsoft.com/fr-fr/aspnet/core/security/authorization/simple?view=aspnetcore-7.0

If I follow the recommendation I've put the [Authorize] attribute as follow

    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class AdtPageController : ControllerBase
    {

        private readonly ApplicationDbContext _context;
        public AdtPageController(ApplicationDbContext context)
        {
            this._context = context;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var devs = await _context.ADTs.ToListAsync();
            return Ok(devs);
        }
...
}

Normally this would specify that I don't have access to this controller. This is not the case! How can I limit access?

Plus, I would like to know how to create an account with access to the entire application.

Thanks in advance

Developer technologies | .NET | Blazor
{count} votes

Your answer

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