2080 12

CSC378 ยท 2080 ยท 15 marks

Write short notes on: (any two) a. Lambda Expression b. Cross Site Request Forgery c. Single Page Application [5+5+5]

Write short notes on: (any two) a. Lambda Expression b. Cross Site Request Forgery c. Single Page Application [5+5+5]

NET Centric ComputingChapter 1: Language PreliminariesLINQ

Quick answer

The browser automatically sends the authenticated cookie along with this request.

Short Notes (Any Two)


a. Lambda Expression

Definition

A lambda expression is an anonymous function that can be used to create delegates or expression tree types. Lambda expressions are a concise way to write inline functions and are heavily used in LINQ (Language Integrated Query) in C#.

Syntax

(parameters) => expression

or for a block body:

(parameters) => { statements; }

Key Characteristics

  • The => operator is called the lambda operator and is read as "goes to".
  • Lambda expressions can have zero, one, or multiple parameters.
  • They can be used wherever a delegate type is expected.
  • They are a core part of LINQ support in C#, alongside query expressions, object initializers, and collections.

Example

// Simple lambda expression
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25

// Lambda with LINQ
int[] scores = new int[] { 97, 92, 83, 60 };
var highScores = scores.Where(score => score > 80);
foreach (int s in highScores)
    Console.WriteLine(s);
// Output: 97 92 83

Why Use Lambda Expressions?

  • Makes code shorter and more readable.
  • Eliminates the need to define a separate named method for simple operations.
  • Integrates seamlessly with LINQ to query collections, databases, XML, and more.
  • Supports functional programming style in C#.

b. Cross Site Request Forgery (CSRF)

Definition

Cross-Site Request Forgery (CSRF) is a web security attack where an attacker tricks an authenticated user into unknowingly submitting a malicious request to a web application on which the user is currently authenticated.

How CSRF Works

  1. The user logs into a trusted website (e.g., a banking site) and a session cookie is stored in the browser.
  2. Without logging out, the user visits a malicious website.
  3. The malicious site contains a hidden request (e.g., a form or image tag) that sends a request to the trusted website.
  4. Since the browser automatically includes the session cookie, the trusted website processes the request as if it came from the legitimate user.

Example Attack Scenario

<!-- Malicious page sends a hidden request to the bank -->
<img src="http://bank.com/transfer?amount=10000&to=attacker" />

Difference from XSS

FeatureXSSCSRF
Attack targetOther users via injected scriptsAuthenticated user's session
MechanismInjects malicious scripts into pagesForges requests using existing session
Trust exploitedUser trusts the websiteWebsite trusts the user's browser

Prevention Techniques

  • Anti-CSRF Tokens: Include a unique, secret token in every form that the server validates.
  • SameSite Cookie Attribute: Restricts cookies from being sent with cross-site requests.
  • Re-authentication: Require password confirmation for sensitive actions.
  • Checking Referer/Origin headers: Validate that requests originate from the expected domain.

c. Single Page Application (SPA)

Definition

A Single Page Application (SPA) is a web application that loads a single HTML page and dynamically updates the content as the user interacts with the app, without requiring a full page reload from the server.

How SPA Works

  • On the first request, the server sends a single HTML page along with all necessary JavaScript and CSS.
  • Subsequent interactions (navigation, data loading) are handled via AJAX calls to the server, which returns only data (typically JSON), not full HTML pages.
  • The browser's JavaScript framework (e.g., Angular, React, Vue.js) updates the DOM dynamically.

Architecture Diagram

User Action --> JavaScript (Client Side) --> AJAX Request --> Server API
                        |                                         |
                   DOM Updated <-------------- JSON Response -----+

Key Characteristics

  • Only one full page load occurs (at the start).
  • Uses client-side routing to simulate navigation between pages.
  • Heavy use of JavaScript frameworks like Angular, React, or Vue.js.
  • Communicates with the backend via REST APIs or GraphQL.

Advantages

AdvantageDescription
Fast and responsiveNo full page reloads; only data is exchanged
Better user experienceFeels like a desktop application
Reduced server loadServer only sends data, not full HTML
Separation of concernsFrontend and backend are clearly separated

Disadvantages

  • SEO challenges: Search engines may struggle to index dynamically loaded content.
  • Initial load time can be slow as all JavaScript is loaded upfront.
  • Browser history management requires extra handling.
  • Security: Requires careful handling of authentication tokens (e.g., JWT).

Example Technologies

  • Frameworks: Angular, React, Vue.js
  • Communication: AJAX, Fetch API, Axios
  • Routing: React Router, Angular Router

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.