📄 UnAuthorizedController.cs Documentation


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. UML Class Diagram
  5. Source Code Breakdown
  6. View & Routing Conventions
  7. Error Handling & Return Types
  8. Sample Configuration & Usage
  9. 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

  1. Routing & Action

    • Exposes a single Index() action
    • Automatically maps to /UnAuthorized or /UnAuthorized/Index
  2. View Resolution

    • Returns a ViewResult, instructing MVC to render Views/UnAuthorized/Index.cshtml
  3. 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 an ActionResult, specifically a ViewResult by 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 that IrbyImaging.Models can be removed if unused, or used for passing view models later.
  • Line 5:
    Declares the UI.Controllers namespace—keeps controllers logically separated from models and services.
  • Line 7:
    Inherits from Controller, gaining access to MVC features (View(), RedirectToAction(), etc.).
  • Line 10:
    The single action Index() 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 UnAuthorized under Views, then add Index.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 even JsonResult.
  • 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! 🚀