0% found this document useful (0 votes)
1 views5 pages

Using System

This document outlines the setup of a Quiz Application REST API using ASP.NET Core. It includes configurations for JWT authentication, Swagger documentation, and CORS policies, along with database context setup for SQL Server. The application also auto-migrates the database on startup and sets up the middleware pipeline for handling requests.

Uploaded by

pm7356435
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Using System

This document outlines the setup of a Quiz Application REST API using ASP.NET Core. It includes configurations for JWT authentication, Swagger documentation, and CORS policies, along with database context setup for SQL Server. The application also auto-migrates the database on startup and sets up the middleware pipeline for handling requests.

Uploaded by

pm7356435
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

using System.

Text;

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

using [Link];

var builder = [Link](args);

// ── Services ────────────────────────────────────────────────────────────────

[Link]();

[Link]();

// Swagger with JWT support

[Link](c =>

[Link]("v1", new OpenApiInfo

Title = "QuizApp API",

Version = "v1",

Description = "Online Quiz Application REST API"

});

[Link]("Bearer", new OpenApiSecurityScheme

Description = "JWT Authorization: Bearer {token}",


Name = "Authorization",

In = [Link],

Type = [Link],

Scheme = "Bearer"

});

[Link](new OpenApiSecurityRequirement

new OpenApiSecurityScheme

Reference = new OpenApiReference { Type = [Link], Id =


"Bearer" }

},

[Link]<string>()

});

});

// Database

[Link]<QuizDbContext>(options =>

[Link]([Link]("DefaultConnection")));

// JWT Authentication

var jwtKey = [Link]["Jwt:Key"] ??


"QuizApp_SuperSecret_Key_2024_MinLength32!!";

[Link]([Link])

.AddJwtBearer(options =>
{

[Link] = new TokenValidationParameters

ValidateIssuer = true,

ValidateAudience = true,

ValidateLifetime = true,

ValidateIssuerSigningKey = true,

ValidIssuer = [Link]["Jwt:Issuer"] ?? "QuizApp",

ValidAudience = [Link]["Jwt:Audience"] ?? "QuizAppUsers",

IssuerSigningKey = new SymmetricSecurityKey([Link](jwtKey))

};

});

[Link]();

// CORS — allow frontend

[Link](options =>

[Link]("AllowAll", policy =>

[Link]()

.AllowAnyHeader()

.AllowAnyMethod());

});

// Application Services

[Link]<AuthService>();
[Link]<QuizService>();

// ── App Pipeline ─────────────────────────────────────────────────────────────

var app = [Link]();

// Auto-migrate on startup

using (var scope = [Link]())

var db = [Link]<QuizDbContext>();

[Link]();

[Link]();

[Link](c =>

[Link]("/swagger/v1/[Link]", "QuizApp API v1");

[Link] = "swagger";

});

[Link]("AllowAll");

[Link]();

[Link]();

[Link]();

[Link]();

You might also like