Changes between Version 1 and Version 2 of OtherTopics


Ignore:
Timestamp:
09/09/26 03:35:46 (3 days ago)
Author:
232012
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • OtherTopics

    v1 v2  
    1 Other Topics
    2 SQL Performance
     1= Other topics (Performance, Security, …)
    32
    4 Document performance analysis for each complex query that you have produced:
     3== Performance
    54
    6 Propose indexes that could improve the performance of the query.
    7 Document performance analysis using EXPLAIN PLAN to execute the query before the creation of the indexes, and after the creation of the indexes.
    8 Document whether the indexes were truly used in the execution plan
    9 Conclusion about the performance gains
    10 Security measures
     5== Security
    116
    12 Document the security measures you have introduced in your application related to database access (prevention of SQL injection, prevention of un-authorized access to data, …)
    13 Document the security measures you have introduced in your database related to database access (prevention of SQL injection in dynamic queries, prevention of un-authorized access to data, …)
    14 Other developments
     7{{{#!div style="text-align: justify; width: 100%;"
     8==== Cookie-based Authentication
    159
    16 
     10For authentication in our application, we use ASP.NET Core cookie authentication.
     11
     12After a user successfully logs in, the server creates an authentication cookie containing information about the authenticated user. This allows the application to recognize the user on subsequent requests without requiring them to log in again for every request.
     13
     14Cookie authentication is configured in {{{Program.cs}}}:
     15
     16{{{
     17builder.Services
     18    .AddAuthentication("Cookies")
     19    .AddCookie("Cookies", options =>
     20    {
     21        options.LoginPath = "/Account/Login";
     22        options.AccessDeniedPath = "/Account/Login";
     23        options.ExpireTimeSpan = TimeSpan.FromHours(8);
     24        options.SlidingExpiration = true;
     25    });
     26
     27builder.Services.AddAuthorization();
     28}}}
     29
     30The authentication cookie is valid for 8 hours. Sliding expiration is enabled, meaning the authentication period can be renewed while the user remains active.
     31
     32After successful login, claims containing information about the user are created:
     33
     34{{{
     35var claims = new List<Claim>
     36{
     37    new Claim(
     38        ClaimTypes.NameIdentifier,
     39        user.UserId.ToString()),
     40
     41    new Claim(
     42        ClaimTypes.Name,
     43        user.Username),
     44
     45    new Claim(
     46        ClaimTypes.Email,
     47        user.Email),
     48
     49    new Claim(
     50        ClaimTypes.Role,
     51        role)
     52};
     53}}}
     54
     55An identity and authentication principal are then created:
     56
     57{{{
     58var identity = new ClaimsIdentity(
     59    claims,
     60    "Cookies");
     61
     62var principal =
     63    new ClaimsPrincipal(identity);
     64
     65await HttpContext.SignInAsync(
     66    "Cookies",
     67    principal);
     68}}}
     69
     70The role claim allows us to distinguish between consumers and administrators and can be used to restrict access to specific functionality.
     71
     72When the user logs out, the authentication cookie is invalidated:
     73
     74{{{
     75await HttpContext.SignOutAsync("Cookies");
     76
     77HttpContext.Session.Clear();
     78}}}
     79
     80==== Password Storage
     81
     82For password hashing, we use ASP.NET Core's {{{PasswordHasher<User>}}}:
     83
     84{{{
     85builder.Services.AddScoped<
     86    IPasswordHasher<User>,
     87    PasswordHasher<User>>();
     88}}}
     89
     90When a new user registers, their password is hashed before it is stored in the database:
     91
     92{{{
     93user.Password =
     94    _passwordHasher.HashPassword(
     95        user,
     96        model.Password);
     97
     98_context.Users.Add(user);
     99_context.SaveChanges();
     100}}}
     101
     102Because password hashing is a one-way operation, the original password cannot be obtained from the stored value.
     103
     104During login, we first retrieve the user by username:
     105
     106{{{
     107var user = _context.Users
     108    .FirstOrDefault(x =>
     109        x.Username == model.Username);
     110}}}
     111
     112The entered password is then verified against the stored password hash:
     113
     114{{{
     115var result =
     116    _passwordHasher.VerifyHashedPassword(
     117        user,
     118        user.Password,
     119        model.Password);
     120
     121if (result == PasswordVerificationResult.Failed)
     122{
     123    ModelState.AddModelError(
     124        "",
     125        "Invalid username or password.");
     126
     127    return View(model);
     128}
     129}}}
     130
     131This allows the application to verify a password without ever storing or comparing plaintext passwords in the database.
     132
     133==== Protection Against CSRF
     134
     135For POST requests that modify application data, ASP.NET Core anti-forgery protection is used.
     136
     137Controller actions that receive POST requests are marked with:
     138
     139{{{
     140[HttpPost]
     141[ValidateAntiForgeryToken]
     142}}}
     143
     144For example:
     145
     146{{{
     147[HttpPost]
     148[ValidateAntiForgeryToken]
     149public IActionResult Register(RegisterViewModel model)
     150{
     151    // ...
     152}
     153}}}
     154
     155The anti-forgery token protects the application against Cross-Site Request Forgery (CSRF) attacks by ensuring that the submitted request originates from a valid application form.
     156
     157==== HTTPS and HSTS
     158
     159The application redirects HTTP requests to HTTPS:
     160
     161{{{
     162app.UseHttpsRedirection();
     163}}}
     164
     165Additionally, outside the development environment, HTTP Strict Transport Security (HSTS) is enabled:
     166
     167{{{
     168if (!app.Environment.IsDevelopment())
     169{
     170    app.UseExceptionHandler("/Home/Error");
     171    app.UseHsts();
     172}
     173}}}
     174
     175HTTPS protects communication between the browser and the server by encrypting transmitted information, while HSTS instructs browsers to use HTTPS when communicating with the application.
     176}}}