WebTechnology With .NET Unit-2
WebTechnology With .NET Unit-2
with .NET
C# Features
Dharmendra Bhatti 1
C# Features
Dharmendra Bhatti 2
Dharmendra Bhatti 3
Dharmendra Bhatti 4
lvar builder =
WebApplication.CreateBuilder(args);
lbuilder.Services.AddControllersWithVie
ws();
lvar app = builder.Build();
Dharmendra Bhatti 5
Dharmendra Bhatti 7
Dharmendra Bhatti 8
Dharmendra Bhatti 9
ldotnet run
Dharmendra Bhatti 10
Dharmendra Bhatti 11
Dharmendra Bhatti 13
Dharmendra Bhatti 14
Dharmendra Bhatti 15
Dharmendra Bhatti 16
nullable
Dharmendra Bhatti 17
nullable
Dharmendra Bhatti 18
nullable
Dharmendra Bhatti 19
Dharmendra Bhatti 20
Dharmendra Bhatti 22
Dharmendra Bhatti 25
Dharmendra Bhatti 26
Dharmendra Bhatti 27
Dharmendra Bhatti 28
Dharmendra Bhatti 29
Dharmendra Bhatti 30
Dharmendra Bhatti 31
Dharmendra Bhatti 32
Dharmendra Bhatti 33
Dharmendra Bhatti 34
Dharmendra Bhatti 35
Dharmendra Bhatti 36
Dharmendra Bhatti 37
Dharmendra Bhatti 38
Dharmendra Bhatti 39
Dharmendra Bhatti 40
Dharmendra Bhatti 41
Pattern Matching
lThe “is” keyword performs a type check
and, if a value is of the specified type, will
assign the value to a new variable
Dharmendra Bhatti 42
Pattern Matching
lPattern Matching in switch Statements
Dharmendra Bhatti 43
Dharmendra Bhatti 44
Dharmendra Bhatti 45
Dharmendra Bhatti 46
Dharmendra Bhatti 47
Dharmendra Bhatti 48
Dharmendra Bhatti 49
Dharmendra Bhatti 50
Dharmendra Bhatti 51
Dharmendra Bhatti 52
Dharmendra Bhatti 53
Dharmendra Bhatti 54
Dharmendra Bhatti 57
Dharmendra Bhatti 58
Dharmendra Bhatti 59
Dharmendra Bhatti 60
Dharmendra Bhatti 61
Dharmendra Bhatti 62
Dharmendra Bhatti 63
Dharmendra Bhatti 64
Dharmendra Bhatti 65
Dharmendra Bhatti 66
Dharmendra Bhatti 67
Dharmendra Bhatti 68
Dharmendra Bhatti 69
Dharmendra Bhatti 70
HTTP
Dharmendra Bhatti 71
HTTP
Dharmendra Bhatti 72
HTTP
Dharmendra Bhatti 73
HTTP
Dharmendra Bhatti 74
HTTP Response
Dharmendra Bhatti 75
Cookies
Dharmendra Bhatti 76
Cookies
Dharmendra Bhatti 77
Cookies
lTypes of Cookies
¡Session Cookies
¡Persistent Cookies
Dharmendra Bhatti 78
Cookies
lSession Cookies:
¡Session cookies are temporary cookies that are
stored on the user's computer for the duration
of their visit to a website.
¡They are typically used to maintain session stat
e and are destroyed when the user closes their
browser or navigates away from the web page.
Dharmendra Bhatti 79
Cookies
lPersistent cookies
¡Persistent cookies are long-
term cookies that can be stored across multiple
sessions.
¡They retain information such as login credential
s or user preferences, allowing users to
have a personalized experience when they revi
sit a website.
Dharmendra Bhatti 80
Cookies
lWriting a Cookie:
¡CookieOptions options = new CookieOptions();
¡options.Expires = DateTime.Now.AddDays(7);
¡Response.Cookies.Append("somekey",
"somevalue", options);
Dharmendra Bhatti 81
Cookies
lReading a Cookie:
lstring cookieValue =
Request.Cookies["Key"];
Dharmendra Bhatti 82
Cookies
Cookies
lDeleting a Cookie:
¡Response.Cookies.Delete("somekey");
Dharmendra Bhatti 84
Cookies
lProgram.cs
Dharmendra Bhatti 85
Cookies
lProgram.cs
Dharmendra Bhatti 86
Cookies
lProgram.cs
Dharmendra Bhatti 87
Cookies
lCookieOptions Properties
¡Domain: This property specifies the hosts to
which the browser will send the cookie. By
default, the cookie will be sent only to the host
that created the cookie.
¡Expires: This property sets the expiry for the
cookie.
¡HttpOnly: When true, this property tells the
browser not to include the cookie in requests
made by JavaScript code.
Dharmendra Bhatti 88
Cookies
lCookieOptions Properties
¡IsEssential: This property is used to indicate
that a cookie is essential, as described in the
“Managing Cookie Consent” section.
¡MaxAge: This property specifies the number of
seconds until the cookie expires. Older
browsers do not support cookies with this
setting.
¡Path: This property is used to set a URL path
that must be present in the request before the
cookie will be sent by the
Dharmendra browser.
Bhatti 89
Cookies
lCookieOptions Properties
¡SameSite: This property is used to specify
whether the cookie should be included in cross-
site requests. The values are Lax, Strict, and
None (which is the default value).
¡Secure: When true, this property tells the
browser to send the cookie using HTTPS only.
Dharmendra Bhatti 90
Cookies
Dharmendra Bhatti 91
Cookies
Dharmendra Bhatti 92
Cookies
Dharmendra Bhatti 93
Cookies
Dharmendra Bhatti 94
Cookies
Dharmendra Bhatti 95
ConsentMiddleware.cs
Dharmendra Bhatti 96
Cookies – Example1
l public class CookiesController : Controller
{
public IActionResult Index()
{
if(!HttpContext.Request.Cookies.ContainsKey("first_request"))
{
HttpContext.Response.Cookies.Append("first_request", DateTime.Now.ToString());
return Content("Welcome, new visitor!");
}
else
{
DateTime firstRequest =
DateTime.Parse(HttpContext.Request.Cookies["first_request"]);
return Content("Welcome back, user! You first visited us on: " +
firstRequest.ToString());
}
}
}
Dharmendra Bhatti 97
Cookies – Example1
Dharmendra Bhatti 98
Cookies – Example2 -
HomeController.cs
Dharmendra Bhatti 99
Cookies – Example2 -
HomeController.cs
Cookies – Example3 -
HomeController.cs
Cookies – Example3 -
HomeController.cs
Cookies – Summary
Cookies – Summary
State Management
lCookies
lSession
lTempData
lQuery strings
lHidden fields
lCache
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
Sessions
lAddDistributedMemoryCache
¡This method sets up an in-memory cache.
¡Despite the name, the cache is not distributed
and is responsible only for storing data for the
instance of the ASP.NET Core runtime where it
is created.
lAddDistributedSqlServerCache
¡This method sets up a cache that stores data in
SQL Server and is available when the
Microsoft.Extensions.Caching.SqlServer
package is installed.
lAddStackExchangeRedisCache
¡This method sets up a Redis cache and is
available when the Microsoft.
Extensions.Caching.Redis package is installed.
Sessions
Session – Example1 -
HomeController.cs
Session – Example1 -
HomeController.cs
Session – Example2 -
HomeController.cs
Session – Example2 -
HomeController.cs
Example – Form1
Example – Form1
lHomeController.cs
Example – Form1
lIndex.cshtml
Hidden Field
Example – HiddenExample1
Example – HiddenExample1
lHomeController.cs
Example – HiddenExample1
lIndex.cshtml
Example – HiddenExample1
lPrivacy.cshtml
lAdvantages:
lEncryption
lSite Verification
lCustomer Trust (Green Lock)
lSEO
lLimitations:
lCost
lPerformance
lCaching
lHttpContext.Request.IsHttps
HttpsExample1 - Program.cs
lbuilder.Services.AddSingleton<IHttpConte
xtAccessor, HttpContextAccessor>();
l…
l//app.UseHttpsRedirection();
lapp.UseStaticFiles();
HttpsExample1 -
Properties/launchSettings.json
HttpsExample1 - Index.cshtml
lapp.UseHttpsRedirection();
l...
lbuilder.Services.AddHttpsRedirection(opts
=> {
¡opts.RedirectStatusCode =
StatusCodes.Status307TemporaryRedirect;
¡opts.HttpsPort = 443;
l});
l...
Dharmendra Bhatti 164
lPossible Attacks:
¡protocol downgrade
¡cookie hijacking attacks
l<VirtualHost www.example.com:80>
¡Header always set Strict-Transport-Security
"max-age=63072000; includeSubdomains;
preload"
l</VirtualHost>
Dharmendra Bhatti 174
ExceptionExample1 - Program.cs
l…
l app.Run(context => {
l throw new Exception("Something went
wrong!");
l });
l app.Run();
ExceptionExample1
ExceptionExample1 -
Properties/launchSettings.json
lIn Properties/launchSettings.json file,
change
¡"ASPNETCORE_ENVIRONMENT": "Development”
¡to
¡"ASPNETCORE_ENVIRONMENT": ”Production"
ExceptionExample1
lif (!app.Environment.IsDevelopment())
l{
l app.UseExceptionHandler("/error.html");
l app.UseHsts();
l}
ExceptionExample1 -
Properties/launchSettings.json
lIn Properties/launchSettings.json file,
change
¡"ASPNETCORE_ENVIRONMENT": "Development”
¡to
¡"ASPNETCORE_ENVIRONMENT": ”Production"
lhttps://localhost:7124/error
Questions ???