-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from chaitanya-jvnm/create-csharp-snippets
feat: Adding CSharp Snippets
- Loading branch information
Showing
8 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
title: Hello, World! | ||
description: Prints Hello, World! to the terminal. | ||
author: chaitanya-jvnm | ||
tags: c#,printing,hello-world,utility | ||
--- | ||
|
||
```c# | ||
public class Program { | ||
public static void Main(string[] args) { | ||
System.Console.WriteLine("Hello, World!"); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Hello, World! | ||
description: Generates a new GUID | ||
author: chaitanya-jvnm | ||
tags: c#,guid,generate,utility | ||
--- | ||
|
||
```c# | ||
public static string GenerateGuid() { | ||
return Guid.NewGuid().ToString(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Hello, World! | ||
description: Checks if a string is a valid GUID. | ||
author: chaitanya-jvnm | ||
tags: c#,guid,validate,utility | ||
--- | ||
|
||
```c# | ||
public static bool IsGuid(string str) { | ||
return Guid.TryParse(str, out _); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Hello, World! | ||
description: Decodes a JWT. | ||
author: chaitanya-jvnm | ||
tags: c#,jwt,decode,utility | ||
--- | ||
|
||
```c# | ||
/// <summary> | ||
/// Decodes the JWT | ||
/// <summary> | ||
public static string DecodeJwt(string token) { | ||
return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString(); | ||
} | ||
|
||
//Example | ||
string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; | ||
|
||
string decodedJwt = DecodeJwt(token); | ||
|
||
Console.WriteLine(decodedJwt); //Prints {"alg":"HS256","typ":"JWT"}.{"sub":"1234567890","name":"John Doe","iat":1516239022} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: Hello, World! | ||
description: Generates a new JWT. | ||
author: chaitanya-jvnm | ||
tags: c#,jwt,generate,utility | ||
--- | ||
|
||
```c# | ||
public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) { | ||
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)); | ||
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); | ||
var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials); | ||
return new JwtSecurityTokenHandler().WriteToken(token); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
title: Hello, World! | ||
description: Validates a JWT. | ||
author: chaitanya-jvnm | ||
tags: c#,jwt,validate,utility | ||
--- | ||
|
||
```c# | ||
public static bool ValidateJwt(string token, string secret) { | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var validationParameters = new TokenValidationParameters { | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)), | ||
ValidateIssuer = false, | ||
ValidateAudience = false | ||
}; | ||
try { | ||
tokenHandler.ValidateToken(token, validationParameters, out _); | ||
return true; | ||
} | ||
catch { | ||
return false | ||
} | ||
} | ||
|
||
//Example | ||
string JWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; | ||
|
||
string correctSecret = "your-256-bit-secret"; | ||
string wrongSecret = "this-is-not-the-right-secret"; | ||
|
||
Console.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True | ||
Console.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False | ||
``` |
20 changes: 20 additions & 0 deletions
20
snippets/csharp/string-utilities/capitalize-first-letter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Hello, World! | ||
description: Makes the first letter of a string uppercase. | ||
author: chaitanya-jvnm | ||
tags: c#,string,capitalize,utility | ||
--- | ||
|
||
```c# | ||
/// <summary> | ||
/// Capitalize the first character of the string | ||
/// <summary> | ||
public static string Capitalize(this string str) { | ||
return str.Substring(0, 1).ToUpper() + str.Substring(1); | ||
} | ||
|
||
//Example | ||
string example = "hello"; | ||
string captializedExample = example.Capitalize(); | ||
Console.WriteLine(captializedExample); // prints "Hello" | ||
``` |