Search This Blog
Saturday, November 28, 2020
Q16-Q20
Q11-Q15
[Authorize]. eg Program.cs is the entry point of the application. It uses the minimal hosting model, so there’s no longer a separate Startup.cs. Everything — dependency injection (DI), middleware, configuration, and routing — is set up directly in Program.cs.Typical Usage:
-
Create the builder and register services
-
builder.Servicesis used to register dependencies, controllers, authentication, EF DbContext, etc.
var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddDbContext<MyDbContext>(); builder.Services.AddAuthentication(...).AddJwtBearer(...); -
-
Build the application
var app = builder.Build(); -
Configure the middleware pipeline
-
Middleware like authentication, authorization, exception handling, static files, Swagger, etc. are configured in order.
app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); -
-
Map endpoints
-
Controllers, minimal APIs, health checks, etc. are wired here.
app.MapControllers(); app.MapGet("/hello", () => "Hello from .NET 8!"); -
-
Run the app
app.Run();
Wednesday, November 18, 2020
My Other Blogs
- Azure http://himanshugoel2018azure.blogspot.com/
- Angular 9 https://himanshugoel2018angular.blogspot.com/
- SQL https://himanshugoel2018sql.blogspot.com/
- .net Core http://himanshugoel2018core.blogspot.com/
- C-Sharp https://himanshugoel2018csharp.blogspot.com/
- WebAPI https://himanshugoel2018webapi.blogspot.com/
- Microservices https://himanshugoel2018microservices.blogspot.com/
- MVC http://himanshugoel2018mvc.blogspot.com/
- HTML + CSS https://himanshugoel2018css.blogspot.com/
- JavaScript ES6 https://himanshugoel2018javascript.blogspot.com/
- Learning Habits https://himanshugoel2018learninghabit.blogspot.com/
- Learning Topics https://himanshugoel2018learningtopics.blogspot.com/
- Managerial https://himanshugoel2018managerial.blogspot.com/
- Node JS https://himanshugoel2018nodejs.blogspot.com/
- Softmind http://softmindit.blogspot.com/
Thursday, November 5, 2020
Q6-Q10
Program.cs (in .NET 6+) or Startup.cs (older versions).We use methods like:
services.AddSingleton<IMyService, MyService>();services.AddScoped<IMyService, MyService>();services.AddTransient<IMyService, MyService>();
Types of Service Lifetimes
Singleton 🟢
- Created only once for the entire application lifetime.
- Same instance is shared across all requests and users.
- Example: Caching service, configuration reader.
- Created once per request (scope).
- The same instance is reused within a single request but a new one is created for each new request.
- Example: Database context (DbContext) in web APIs.
- Created every time it’s requested.
- Best for lightweight, stateless services.
- Example: Utility services like email formatters, logging helpers.
<AspNetCoreHostingModel> property to InProcess in the project file (.csproj)<AspNetCoreHostingModel> property to OutOfProcess in the project file (.csproj):Answer:
Sunday, October 25, 2020
Q1-Q5
app.Run) can short-circuit the pipeline and it will stop the further calling. UseRouting, UseAuthentication, UseAuthorization) or create custom middleware by implementing a class with an Invoke or InvokeAsync method.Program.cs file using extension methods like app.Use(...), app.Map(...), app.MapWhen(...)or app.Run(...).1. Authentication (Identity Establishment)
-
A client first authenticates by presenting credentials—this could be username/password through a login API, an API key, or a federated login (e.g., OpenID Connect).
-
If credentials are valid, the server issues a security token (commonly a JWT). The token contains identity details and claims (e.g., user id, email, roles).
{"sub": "12345","name": "Himanshu Goel","role": "Admin","exp": 1727467200}
- The client stores the token (e.g., in local storage, session storage, or cookies).
- For subsequent API calls, the client includes the token in the
Authorizationheader: - Authorization: Bearer <token>
-
The Authentication middleware validates the token (signature, expiry, issuer, audience). If invalid, the request is rejected.
-
Once authenticated, the Authorization middleware inspects the claims to enforce policies, roles, or custom requirements. Controllers or endpoints decorated with
[Authorize]are protected, while[AllowAnonymous]bypasses checks.
[Authorize(Roles = "Admin")]public IActionResult GetAdminData() { ... }
var builder = WebApplication.CreateBuilder(args);builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options => {
options.TokenValidationParameters = ...;
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
for app.use() and app.next()What it does: Adds a middleware to the pipeline.That middleware can do some work before passing the request along, and then also do work after the next middleware finishes.Example: logging, authentication, error handling.app.Use(async (context, next) =>{Console.WriteLine("Before next middleware");await next(); // pass control to the next middlewareConsole.WriteLine("After next middleware");});
- It’s decorated with
[ApiController]or[Controller], or - It has routing attributes (
[Route],[HttpGet], etc.), or - It inherits from
ControllerorControllerBase.
Program.cs when registering controllers.Q36-Q40(EF)
Q36. What is the difference between Include and Join in entity framework? Q37. What will be your approach in case you want to call a manual...
-
Q1 . What is middleware in dotnet 8 ? Q2.What is the High-Level Flow of Authentication and Authorization in dotnet 8 applications? Q3. Diff...
-
Q11. What is [ AllowAnonymous] Attribute in dotnet 8? Q12. What are the benefits of .net core application over asp.net applications? Q13. D...
-
Steps to use EF Core in project. (easy) >> Go to Nuget packages and install "Microsoft.EntityFramework.SqlServer". This wil...

