📄 Documentation: InfoController.cs


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Controller Inheritance & Routing
  4. API Endpoints Overview
  5. Data Models & View Models
  6. Helper Methods & Utilities
  7. Error Handling & Return Types
  8. Third‑party Integrations
  9. UML Class Diagram
  10. Sample Code Blocks
  11. Usage Examples
  12. Conclusion

1. Overview & Purpose

The InfoController is a simple ASP.NET Core API controller residing in the IrbyImaging project. Its primary purpose is to expose a diagnostic endpoint that returns details about the authenticated user’s claims. This is often used in UI debugging or to verify that the authentication middleware is functioning as expected.

🚀 Key Goal: Provide a JSON payload of the current user’s claims for diagnostic or UI display purposes.


2. Dependencies & Technologies

Technology / Package Purpose
Microsoft.AspNetCore.Mvc Web API controller base classes & attributes
Microsoft.AspNetCore.Http HTTP context & user identity
Microsoft.Extensions.Configuration Access to application configuration settings
Newtonsoft.Json JSON serialization
IrbyImaging.UI.Controllers BaseController class inheritance
IrbyImaging.Context (IrbyContext) Entity Framework Core DbContext for database access

3. Controller Inheritance & Routing

namespace IrbyImaging.Controllers
{
    [Route("info")]
    public class InfoController : BaseController
    {
        // ...
    }
}
  • Inheritance
    InfoController inherits from a shared BaseController, which itself likely extends ControllerBase and provides common functionality (e.g., access to User, localization, common error handling, etc.).

  • Routing

    • The [Route("info")] attribute on the controller sets the base route to /info.
    • Individual actions append to this route (e.g., GET /info/auth).

4. API Endpoints Overview

GET /info/auth

  • URL: /info/auth
  • HTTP Method: GET
  • Description: Returns a JSON document containing the authenticated user’s claims.
  • Response Content-Type: application/json
  • Authentication: Relies on ASP.NET Core’s authentication middleware. If the user is unauthenticated, the claims collection will be empty.

Signature

[HttpGet("auth")]
public ActionResult Auth()

Workflow

  1. Instantiate a Dictionary<string, object> to hold result data.
  2. Populate the "Claims" key by projecting each Claim in User.Claims to an anonymous object with Name and Value.
  3. Serialize the result to indented JSON using JsonConvert.
  4. Return content as application/json.

5. Data Models & View Models

This controller uses only simple in-memory .NET objects:

  • Dictionary<string, object>
    A dynamic container for key/value pairs.

  • Anonymous Object
    { Name = x.Type, Value = x.Value } for each user claim.

No dedicated ViewModels or DataModels are defined in this code file.


6. Helper Methods & Utilities

  • Serialization:
    Uses Newtonsoft.Json.JsonConvert.SerializeObject(...) to produce human-readable JSON.

  • User Claims Projection:
    LINQ is used to transform Claim objects into a lightweight projection.

Data["Claims"] = this.User.Claims
    .Select(x => new { Name = x.Type, Value = x.Value });

7. Error Handling & Return Types

  • Return Type: ActionResult
    Enables returning different HTTP status codes or content types.

  • Error Handling

    • No explicit try/catch is implemented.
    • Unhandled exceptions bubble up to ASP.NET Core’s global error handler.

8. Third-party Integrations

  • Newtonsoft.Json
    • Provides advanced JSON serialization settings and formatting.
    • Installed via NuGet (Newtonsoft.Json).

9. UML Class Diagram

classDiagram
    class BaseController {
        <<abstract>>
        +ClaimsPrincipal User
        +IConfiguration Configuration
        +IActionResult Content(string, string)
        // ... other shared methods/properties
    }
    class InfoController {
        -IrbyContext _context
        -IConfiguration _configuration
        +Auth() ActionResult
    }
    BaseController <|-- InfoController

📑 Note: BaseController is part of IrbyImaging.UI.Controllers and is not shown here in full.


10. Sample Code Blocks

InfoController.cs

[Route("info")]
public class InfoController : BaseController
{
    private readonly IrbyContext _context;
    private readonly IConfiguration _configuration;

    public InfoController(IrbyContext context, IConfiguration configuration)
        : base(context, configuration)
    {
        _context = context;
        _configuration = configuration;
    }

    [HttpGet("auth")]
    public ActionResult Auth()
    {
        var data = new Dictionary<string, object>();
        data["Claims"] = this.User.Claims
            .Select(x => new { Name = x.Type, Value = x.Value });

        var json = JsonConvert.SerializeObject(data, Formatting.Indented);
        return this.Content(json, "application/json");
    }
}

11. Usage Examples

1. Curl

curl -X GET "https://your-api.com/info/auth" \
     -H "Authorization: Bearer <your-jwt-token>"

Sample Response

{
  "Claims": [
    { "Name": "sub", "Value": "1234567890" },
    { "Name": "name", "Value": "Jane Doe" },
    { "Name": "role", "Value": "Administrator" }
  ]
}

2. JavaScript (Fetch)

fetch('/info/auth', {
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
.then(res => res.json())
.then(data => console.table(data.Claims))
.catch(console.error);

12. Conclusion

The InfoController is a lightweight diagnostic endpoint in the IrbyImaging application. It demonstrates best practices in:

  • Controller Routing & Inheritance (via BaseController)
  • Dependency Injection of DbContext & IConfiguration
  • User Claims Extraction & JSON Serialization

This controller can be extended to expose additional environment details, configuration flags, or health-check data as required. Use it as a template for exposing read-only informational endpoints in your ASP.NET Core Web APIs.


❤️ Happy Coding!