ASP.NET Core 9.0 Web API application with authentication, user management, and authorization features.
Here’s a breakdown of the functionality in this class (Program.cs), which configures an ASP.NET Core Web API application with authentication, user management, and authorization features. The code is centered around Google OAuth and role-based user management.
1. Services Configuration
Minimal asp.net core API
Scans and discovers all Minimal API endpoints (routes added via methods like MapGet, MapPost, etc.).
csharp
builder.Services.AddEndpointsApiExplorer();
Swagger Setup
- What it does: Adds Swagger support for API documentation.
- Includes a Bearer token security scheme to secure the API endpoints that require authentication.
Authentication Configuration
- Default Scheme: Sets
IdentityConstants.BearerSchemeas the default authentication scheme. - Configures:
- Cookies for user session management.
- Google OAuth using
.AddGoogle()for social login. - Bearer Token Authentication for issuing and validating tokens.
Identity Setup
- Adds ASP.NET Core Identity services for managing users and roles.
- Configures
ApplicationDbContext(a SQLite database) for storing user data. - Enables token-based authentication and role-based policies.
Database Setup
- Configures SQLite as the database provider.
Authorization Policies
- AdminPolicy: Requires the user to have an "Admin" role.
- UserPolicy: Requires the user to have a "User" role.
2. Middleware Configuration
Development Mode
- Enables Swagger for API testing in development mode:
HTTPS Redirection
- Forces HTTPS for all API endpoints:
Identity API Mapping
- Maps Identity API endpoints for managing users (e.g., register, login):
3. API Endpoints
Google OAuth Login
- Endpoint:
/mauth/google - What it does:
- Initiates Google OAuth login by redirecting the user to Google.
- Configures
AuthenticationPropertieswith a callback to/mauth/google/callback.
Google OAuth Callback
- Endpoint:
/mauth/google/callback - What it does:
- Handles the OAuth callback from Google.
- Authenticates the user using cookies.
- Creates a new user if the user does not exist in the database.
- Issues a Bearer token and redirects to the app with token data.
Register a User
- Endpoint:
/registerUser - What it does:
- Creates a new user in the database with the role "User."
Delete a User
- Endpoint:
/users/{email} - What it does:
- Deletes a user from the database.
- Requires the "AdminPolicy."
Check If User Can Delete
- Endpoint:
/users/candelete - What it does:
- Checks if the logged-in user has the "Admin" role.
Get Current User Info
- Endpoint:
/me - What it does:
- Returns the logged-in user's email and birth date.
- Requires authentication.
List All Users
- Endpoint:
/users - What it does:
- Returns a list of all users with their emails and birth dates.
- Requires authentication.
4. Database Initialization
- Creates an admin user and several test users during application startup.
Key Functionalities
- Google OAuth: Allows users to log in via Google.
- Role-Based Access Control: Admin and user roles with authorization policies.
- Token Authentication: Issues and validates Bearer tokens for securing API endpoints.
- CRUD Operations on Users: Register, delete, list users, and check authorization.
- Swagger Documentation: Enables API testing and exploration in development.
Comments
Post a Comment