📄 UnAuthorizedController.cs Documentation
📑 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- UML Class Diagram
- Source Code Breakdown
- View & Routing Conventions
- Error Handling & Return Types
- Sample Configuration & Usage
- Conclusion
🔍 Overview & Purpose
The UnAuthorizedController is a simple ASP.NET Core MVC controller in the IrbyImaging.UI.Controllers namespace. Its primary responsibility is to handle unauthorized access attempts and return a friendly “Unauthorized” view when a user navigates to a protected resource without the necessary permissions.
-
Use Case
- Display a user-friendly page (e.g., HTTP 401 or 403) when authentication or authorization fails.
- Central place to customize the look, feel, and messaging for unauthorized users.
-
Intended Audience
- Front‑end developers customizing the unauthorized view
- Back‑end developers wiring up custom error pages in the ASP.NET Core pipeline
🔧 Dependencies & Technologies
| Category | Details |
|---|---|
| .NET Version | ASP.NET Core (2.x, 3.x, 5+, or later) |
| Primary Namespace | Microsoft.AspNetCore.Mvc |
| Project Namespace | IrbyImaging.UI.Controllers |
| Related Models | IrbyImaging.Models (imported but not used) |
| View Engine | Razor Views (.cshtml) |
🛠 Key Functional Areas
-
Routing & Action
- Exposes a single
Index()action - Automatically maps to
/UnAuthorizedor/UnAuthorized/Index
- Exposes a single
-
View Resolution
- Returns a
ViewResult, instructing MVC to renderViews/UnAuthorized/Index.cshtml
- Returns a
-
Separation of Concerns
- No business logic or model manipulation here—purely view orchestration
📐 UML Class Diagram
+---------------------------------------+
| Microsoft.AspNetCore.Mvc |
| Controller |
+---------------------------------------+
▲
|
+---------------------------------------+
| IrbyImaging.UI.Controllers |
| UnAuthorizedController |
+---------------------------------------+
| + Index(): ActionResult |
+---------------------------------------+
Legend:
- Solid arrow ▲ indicates inheritance.
Index()returns anActionResult, specifically aViewResultby default.
📂 Source Code Breakdown
using IrbyImaging.Models; // Reference to project models (not currently used)
using Microsoft.AspNetCore.Mvc; // Core MVC Controller base
namespace IrbyImaging.UI.Controllers
{
public class UnAuthorizedController : Controller
{
// GET: /UnAuthorized/Index
public ActionResult Index()
{
// Returns the default Razor view (Views/UnAuthorized/Index.cshtml)
return View();
}
}
}
- Line 1–2:
Imports namespaces; note thatIrbyImaging.Modelscan be removed if unused, or used for passing view models later. - Line 5:
Declares theUI.Controllersnamespace—keeps controllers logically separated from models and services. - Line 7:
Inherits fromController, gaining access to MVC features (View(),RedirectToAction(), etc.). - Line 10:
The single actionIndex()with[HttpGet]semantics by convention; returns a view for unauthorized access.
🎨 View & Routing Conventions
Convention-Based View Location
When return View(); is called in UnAuthorizedController.Index(), MVC searches for:
/Views/UnAuthorized/Index.cshtml
/Views/Shared/Index.cshtml
Tip: Create a folder named
UnAuthorizedunderViews, then addIndex.cshtml:@* Views/UnAuthorized/Index.cshtml *@ @{ ViewData["Title"] = "Unauthorized"; } <h1>🚫 Access Denied</h1> <p>You do not have permission to view this page.</p>
Custom Routing (Optional)
If you wish to expose a custom route:
[Route("access-denied")]
public ActionResult Index()
{
return View();
}
This maps /access-denied directly to your view.
🚨 Error Handling & Return Types
- Return Type:
ActionResult- Flexible: you can later return
ViewResult,RedirectResult, or evenJsonResult.
- Flexible: you can later return
- Error Handling:
- Currently no try/catch blocks, as there’s no logic prone to exceptions.
- You can add logging or more advanced diagnostics as required.
⚙️ Sample Configuration & Usage
1. Configure in Startup.cs
To route 401/403 status codes to this controller:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Other middleware (StaticFiles, Routing, Authentication, ...)
app.UseStatusCodePagesWithReExecute("/UnAuthorized/Index");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
});
}
UseStatusCodePagesWithReExecute
Captures error status codes (e.g., 401, 403) and re-executes the pipeline at/UnAuthorized/Index.
2. Navigating to the Unauthorized Page
- In your application flow, when an unauthorized access occurs, ensure you return:
return Challenge(); // Triggers 401
// or
return Forbid(); // Triggers 403
ASP.NET Core will then redirect to /UnAuthorized/Index and render your custom view.
📝 Conclusion
The UnAuthorizedController is a lightweight MVC component designed for presenting an “Access Denied” experience. By centralizing unauthorized page logic:
- You maintain a consistent UX for security errors.
- It’s trivial to customize the look and behavior of your error pages.
- You keep your application pipeline clean by delegating status-code handling to this dedicated controller.
Generated by AI Assistant – Your guide to maintainable, user‑friendly unauthorized pages! 🚀