You've created a new ASP.NET Core application with user registration. Now, you want to ensure that only registered users can post comments. Which attribute would you use to implement this restriction?
- [AllowAnonymous]
- [Authorize]
- [Authorize(Roles = "Admin")]
- [Authorize(Roles = "User")]
To restrict access to registered users only, you would use the [Authorize] attribute. This attribute can be applied at the controller or action level and ensures that only authenticated users can access the specified resources. It doesn't require specifying roles, as it already implies authenticated users. [AllowAnonymous] would allow both anonymous and registered users, while [Authorize(Roles = "Admin")] and [Authorize(Roles = "User")] are role-based authorizations and are not suitable for this scenario.
Loading...
Related Quiz
- If you want to customize the response sent back to the client based on the type of exception thrown, which feature of ASP.NET Core would you leverage?
- How does the .NET SDK relate to the .NET runtime in the context of application development and deployment?
- In your ASP.NET Core application, you notice that some middleware is not executing as expected. Considering the middleware pipeline, what could be the potential reason?
- Which directive in _ViewImports.cshtml allows you to set the base class for the Razor views?
- Which of the following best describes the "Code First" approach in Entity Framework Core?