Saturday, 23 November 2024

Remove Duplication in the string

 //Word duplication checking

string s = "eearr";

string duplicatewords=string.Empty;

for (int i = 0; i < s.Length; i++)

{

    if (!(duplicatewords.Contains(s[i].ToString())))

    {

        duplicatewords += s[i];

    }

}

Console.WriteLine(duplicatewords);


Output:


Using LINQ for Duplication

//To Get unique Word alone
var a1 = s.GroupBy(x => x.ToString());

OUTPUT: a1[0]=e ,a1[1]=a,a1[2]=r

//To Get duplicate Values alone
var a = s.GroupBy(x => x.ToString())
              .Where(g => g.Count() > 1)
              .Select(y => y.Key)
              .ToList();

OUTPUT: a[0]=e ,a[1]=r

JWT(JSON Web Token)

What is JWT?

JSON Web Token (JWT) serves as a compact and self-contained mechanism for securely transmitting information between parties as a JSON object.

JWT Structure/Components:

Header: Specifies the token type (JWT) and the signing algorithm (e.g., HMAC SHA256).

Payload: Contains the claims, which are statements about an entity (user) and additional metadata.

Signature: Created by encoding the header and payload with a secret, ensuring the token’s integrity.

JWT in Action:

  • Upon user authentication, the server generates a JWT.
  • This JWT is sent back to the client and stored, often in local storage or an HTTP-only cookie.
  • The client includes this token in the HTTP Authorization header for subsequent requests.
  • The server validates the token and grants access if valid.

Advantages:

Scalability: Due to their stateless nature, JWTs are ideal for distributed systems.

Flexibility: They can be used across different domains and applications.

Security: When properly implemented, they provide a secure way to handle user authentication.

Security Concerns:

Transmission Security: It's vital to transmit JWTs over HTTPS.

Storage: Store JWTs securely to prevent XSS attacks and other vulnerabilities.

Handling Token Expiry:

Implement short-lived JWTs and use refresh tokens for renewing access without re-authentication.