Unit 4: Asp.Net MVC and Security - Practice Quiz
1 What is the middleware request pipeline in ASP.NET Core?
2 Why is the order of middleware components important in ASP.NET Core?
3 Which method can terminate the middleware pipeline by writing a response without calling the next component?
Use
Map
Build
Run
4 Which built-in middleware is commonly used to serve CSS, JavaScript, and image files?
5 Which method is commonly used to add a custom inline middleware component to the pipeline?
app.Build
app.Create
app.Use
app.Start
6 What method is usually called inside custom middleware to pass control to the next component?
ConfigureAsync on the application
RegisterAsync on the service collection
ConnectAsync on the database context
InvokeAsync on the next delegate
7 What is Dependency Injection?
8 What is a key benefit of Dependency Injection?
9 Which type is commonly used to define a dependency contract in C#?
10 Where are application services registered in a modern ASP.NET Core application?
builder.Logging
app.Environment
builder.Services
app.Configuration
11 Which service lifetime creates one instance for each HTTP request?
12 Which method registers a service that uses one instance for the application's lifetime?
AddTransient
AddSingleton
AddControllers
AddScoped
13 In the Code First approach, what is typically created first?
14 What does an Entity Framework Core migration represent?
15 Which command creates a new Entity Framework Core migration?
dotnet ef migrations add
dotnet ef database drop
dotnet ef dbcontext list
dotnet ef migrations remove
16 In the Database First approach, what already exists before entity classes are generated?
17 What is scaffolding in the Entity Framework Core Database First approach?
18 What does authentication determine in a web application?
19 What does authorization determine in a web application?
20 Which ASP.NET Core attribute is commonly used to restrict access to a controller or action?
[Route]
[Authorize]
[ValidateAntiForgeryToken]
[HttpGet]
21
A middleware checks whether an API key is present. If the key is missing, it writes a 401 response and does not call the next middleware. What behavior does this demonstrate?
22
An application should handle exceptions thrown by all later middleware components. Where should UseExceptionHandler normally be placed?
23 Which middleware order correctly enables endpoint-aware authentication and authorization for controller routes?
UseAuthentication, MapControllers, UseRouting, UseAuthorization
UseAuthorization, UseRouting, UseAuthentication, MapControllers
MapControllers, UseRouting, UseAuthorization, UseAuthentication
UseRouting, UseAuthentication, UseAuthorization, MapControllers
24 A custom logging middleware must record both the incoming request and the final response status code. Which implementation pattern should it use?
await _next(context)
_next(context) without awaiting it
_next(context)
25
A class named RequestTimingMiddleware follows the conventional middleware pattern. How should it normally be added to the pipeline?
app.MapControllers<RequestTimingMiddleware>()
app.UseMiddleware<RequestTimingMiddleware>()
app.RunMiddleware<RequestTimingMiddleware>()
builder.Services.Use<RequestTimingMiddleware>()
26
Static files in wwwroot should be served without reaching MVC controllers. Which built-in middleware provides this behavior?
UseAuthorization
UseStaticFiles
UseStatusCodePages
UseRouting
27 A service should maintain one instance during a single HTTP request but use a different instance for the next request. Which DI lifetime is appropriate?
28 A singleton service directly receives a scoped repository through constructor injection. What is the main design problem?
29
Why is injecting IEmailSender generally preferable to constructing SmtpEmailSender directly inside a controller?
30
Which registration creates one OrderService instance per HTTP request when IOrderService is injected?
builder.Services.AddScoped<IOrderService, OrderService>()
builder.Services.AddSingleton<IOrderService, OrderService>()
builder.Services.AddTransient<IOrderService, OrderService>()
builder.Services.Configure<IOrderService, OrderService>()
31
A controller constructor requires IProductRepository, but the application throws an error stating that the service cannot be resolved. What is the most likely correction?
launchSettings.json
app.UseRouting
builder.Services
32
A conventional middleware needs a scoped DbContext for each request. Which approach avoids capturing it for the application's lifetime?
DbContext into InvokeAsync
DbContext in the middleware constructor
DbContext as a singleton
DbContext in a static field
33
A developer adds a Price property to the Product entity in an EF Core Code First application. What should normally be done to update the database schema?
DbContext and restart
34
Which command sequence correctly creates and applies an EF Core migration named AddCategory?
dotnet ef migrations remove, then dotnet ef database update AddCategory
dotnet ef dbcontext scaffold AddCategory, then dotnet ef database drop
dotnet ef migrations add AddCategory, then dotnet ef database update
dotnet ef database update, then dotnet ef migrations add AddCategory
35
An Order entity contains CustomerId and a Customer navigation property. What does EF Core conventionally infer?
CustomerId is a foreign key for the relationship
Customer must be stored as a text column
Order and Customer are unrelated entities
CustomerId is the primary key of Order
36 A database already contains tables and relationships, and the application needs EF Core entity classes generated from it. Which operation should be used?
DbContext and entity classes
37 Which EF Core CLI command is used to reverse-engineer a SQL Server database?
dotnet ef migrations add <connection> Microsoft.EntityFrameworkCore.SqlServer
dotnet ef dbcontext scaffold <connection> Microsoft.EntityFrameworkCore.SqlServer
dotnet ef dbcontext optimize <connection> Microsoft.EntityFrameworkCore.SqlServer
dotnet ef database update <connection> Microsoft.EntityFrameworkCore.SqlServer
38 A developer expects to re-scaffold entity classes after database changes. Where should custom business logic preferably be placed to reduce the risk of losing it?
DbContext
39
A user has successfully signed in but is denied access because an action requires the Admin role. Which security process caused the denial?
40 What is the expected distinction when accessing a protected API endpoint?
41
In an ASP.NET Core pipeline, middleware A calls await _next(context) and then adds a response header. Middleware B, registered after A, writes the response body and also calls its next delegate. Which statement best describes the execution order?
42 A middleware must guarantee that a correlation ID is available to all downstream components and is also included in the final response header. Where should the response-header assignment normally occur?
_next, inside a finally block
_next, inside a try block
_next, after creating the correlation ID
43
A terminal middleware is registered before UseRouting() and writes a 200 OK response without invoking its next delegate. What is the most likely result for endpoint routing?
44 An ASP.NET Core application uses endpoint authorization policies. Which ordering is required for authorization to evaluate the selected endpoint's metadata correctly?
UseRouting() before UseAuthorization()
UseAuthorization() after MapControllers() only
UseRouting() after UseAuthorization() and UseEndpoints()
UseAuthorization() before UseRouting()
45 A custom exception-handling middleware is intended to convert unhandled downstream exceptions into JSON responses. Which implementation detail is essential?
UseExceptionHandler() inside every request
_next(context) with exception handling
_next(context)
46
A custom middleware is registered with app.UseMiddleware<RequestAuditMiddleware>(). The middleware constructor directly receives a repository registered as scoped. What is the principal lifetime risk?
47 A service registered as transient depends on a service registered as scoped. The transient service is resolved repeatedly within one HTTP request. What behavior should be expected?
48
A singleton service directly depends on a scoped DbContext, and scope validation is enabled in development. What is the expected outcome?
49
An interface has three registered implementations, but a consumer requests IEnumerable<IProcessor>. Which result does the default ASP.NET Core container provide?
50
A controller accepts IOptionsSnapshot<AppSettings>, while a singleton background service accepts IOptions<AppSettings>. Configuration is reloaded at runtime. Which statement is correct?
51
A hosted background service needs to query a scoped DbContext every minute. Which design is appropriate?
IServiceScope for each iteration and resolve the context inside it
DbContext from the root provider and reuse it
DbContext directly into the hosted service constructor
DbContext as singleton for background execution
52
A service is registered using services.AddSingleton<IClock, SystemClock>(), and a controller requests SystemClock directly rather than IClock. What happens with the default container?
IClock was registered
SystemClock automatically
53
In EF Core Code First, a required Order.Customer navigation is added to the model, but the migration unexpectedly creates a nullable foreign-key column. Which cause is most plausible?
DbContext
virtual
54 A Code First migration is generated successfully, but applying it fails because a new non-nullable column is added to a populated table without a default. What is the sound migration strategy?
[NotMapped] and retain the migration
55
An EF Core query loads an aggregate, changes a child entity, and calls SaveChanges(). The child was queried through the same tracking DbContext. What usually allows EF Core to persist the child update without an explicit Update call?
SaveChanges() updates only entities attached as Added
56 A database-first EF Core model is scaffolded from a database. A developer manually edits the generated entity class, then scaffolds again after a schema change. What is the safest customization approach?
57 A database-first application must query a view that has no primary key and is read-only. How should the EF Core model represent it?
58
An ASP.NET Core application uses cookie authentication and has an [Authorize(Roles = "Manager")] action. An authenticated user has a valid cookie but lacks the Manager role. What response is expected?
401 Unauthorized because the cookie is invalid
403 Forbidden because the user is authenticated but disallowed
59
A JWT bearer token is validly signed and unexpired, but its aud claim does not match the API's configured audience. What should the API do?
60
A policy requires a claim named scope with value orders.read. The token contains scope as the single string orders.read orders.write. What configuration or transformation is needed for the policy to work as intended?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →