This tutorial assumes you’re building an MVC app that also exposes APIs or wants token-based login.
🔹 Step 1: Create ASP.NET Core MVC Project
🔹 Step 2: Install Required NuGet Packages
🔹 Step 3: Add JWT Settings in appsettings.json
⚠️ Use a long, secure key (store it in User Secrets
or Azure Key Vault
in production).
🔹 Step 4: Configure JWT in Program.cs
🔹 Step 5: Create a Model for Login
Models/LoginModel.cs
🔹 Step 6: Create Token Service
Services/TokenService.cs
🔹 Step 7: Create Authentication Controller
Controllers/AuthController.cs
🔹 Step 8: Protect Your MVC Controllers
Example: Controllers/HomeController.cs
🔹 Step 9: Test the Flow
-
Run the project →
https://localhost:5001/api/auth/login
Send POST request with:Response:
-
Use this token in Authorization Header:
-
Access
https://localhost:5001/home/index
→ works only with valid JWT.
Accesshttps://localhost:5001/home/adminonly
→ works only if role is"Admin"
.
تعليقات
إرسال تعليق
POST Answer of Questions and ASK to Doubt