Net Core Notes
Net Core Notes
2. createdefaultbuilder
4. app.UseDeveloperExceptionPage()
Add Transient (new instance - on request of new instance even in same http
scope or different),ex: OAuthHandler
16. IOptions in .NET core is used to provide strongly type classes to access
appsettings from the application or vault settings defined in the cloud. It
provides encapsulation for that settings to be dependent on configured class and
separated from other settings.
17. EF Core Migrations is to sync between the database schema and application
models
1.Create a new Asp.net core web application and add the following custom
error model:
2.Add a DbLoggerOptions class to store the logger settings:
3.Creating the custom logging provider (ILoggerProvider) - Creates a new
instance of the db logger in the CreateLogger function
_dbLoggerProvider = dbLoggerProvider;
}
//DbLoggerExtensions.cs
public static class DbLoggerExtensions
{
public static ILoggingBuilder AddDbLogger(this
ILoggingBuilder builder, Action<DbLoggerOptions> configure)
{
builder.Services.AddSingleton<ILoggerProvider,
DbLoggerProvider>();
builder.Services.Configure(configure);
return builder;
}
}
// Program.cs
var builder = WebApplication.CreateBuilder(args);
...
builder.Logging.AddDbLogger(options =>
{
builder.Configuration.GetSection("Logging").GetSection("Database").GetSection("Opti
ons").Bind(options);
});
this.auth.loginWithRedirect({
...(this.organization ? { organization:
this.organization } : null),
redirect_uri:
process.CSIRedirectURI+this.csi_uri+?userInfo=${encryptedEmail}
});
by using .... import { AuthService } from
'@auth0/auth0-angular';
2. if user valid then in subsequent calls we send the JWT token in headers
3. At API we validate it with AddAUthentication middleware
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = domain;
options.Audience = audience;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = domain,
ValidateAudience = true,
ValidAudience = audience,
NameClaimType = ClaimTypes.NameIdentifier
};
});
1. create a class and use RequestDelegate and in InvokeAsync Method call next
method
Caching in .NET Core can significantly improve the performance of your application
by reducing the
need to generate the same data repeatedly. .NET Core supports several types of
caching:
1. In-Memory Caching: Stores cache data in the memory of the web server.
(single Server Scenario's) -> Nucor
2. Distributed Caching: Stores cache data across multiple servers or
services. (multiple server Scenario's) -> IOWA
3. Response Caching: Caches responses for HTTP requests.
1.In-Memory Caching:
a. In startup.cs , add in memory caching services --->
services.AddMemoryCache();
b. Inject IMemoryCache into your controller or service and use it to
set and get cache entries:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
_cache.Set(cacheKey, currentTime,
cacheEntryOptions);
}
ViewBag.CurrentTime = currentTime;
return View();
}
}
24. Authorization
Step 1
// Add authorization services
services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
options.AddPolicy("CanEditContent", policy =>
policy.RequireClaim("EditContent"));
});
step 2
app.UseAuthorization();
a. Role-Based Authorization
[Authorize(Roles = "Admin")]
public class AdminController : Controller
{
b. Policy-Based Authorization
[Authorize(Policy = "CanEditContent")]
public class ContentController : Controller
{
c. Claims-Based Authorization
You can add claims to a user and use them for authorization. Here’s an example of
how to set a claim:
await
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new
ClaimsPrincipal(claimsIdentity), authProperties);