Skip to content

Commit

Permalink
Merge pull request #121 from chaitanya-jvnm/create-csharp-snippets
Browse files Browse the repository at this point in the history
feat: Adding CSharp Snippets
  • Loading branch information
Mathys-Gasnier authored Jan 2, 2025
2 parents e74409d + 55bd1e5 commit 641f0ee
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 0 deletions.
106 changes: 106 additions & 0 deletions public/consolidated/csharp.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,97 @@
[
{
"categoryName": "Basics",
"snippets": [
{
"title": "Hello, World!",
"description": "Prints Hello, World! to the terminal.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"printing",
"hello-world",
"utility"
],
"contributors": [],
"code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n"
}
]
},
{
"categoryName": "Guid Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Generates a new GUID",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"guid",
"generate",
"utility"
],
"contributors": [],
"code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n"
},
{
"title": "Hello, World!",
"description": "Checks if a string is a valid GUID.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"guid",
"validate",
"utility"
],
"contributors": [],
"code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n"
}
]
},
{
"categoryName": "Jwt Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Decodes a JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"decode",
"utility"
],
"contributors": [],
"code": "/// <summary>\n/// Decodes the JWT\n/// <summary>\npublic static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n//Example\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring decodedJwt = DecodeJwt(token);\n\nConsole.WriteLine(decodedJwt); //Prints {\"alg\":\"HS256\",\"typ\":\"JWT\"}.{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"iat\":1516239022}\n"
},
{
"title": "Hello, World!",
"description": "Generates a new JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"generate",
"utility"
],
"contributors": [],
"code": "public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {\n var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));\n var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);\n var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);\n return new JwtSecurityTokenHandler().WriteToken(token);\n}\n"
},
{
"title": "Hello, World!",
"description": "Validates a JWT.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"jwt",
"validate",
"utility"
],
"contributors": [],
"code": "public static bool ValidateJwt(string token, string secret) {\n var tokenHandler = new JwtSecurityTokenHandler();\n var validationParameters = new TokenValidationParameters {\n ValidateIssuerSigningKey = true,\n IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),\n ValidateIssuer = false,\n ValidateAudience = false\n };\n try {\n tokenHandler.ValidateToken(token, validationParameters, out _);\n return true;\n }\n catch {\n return false\n }\n}\n\n//Example\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\n\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nConsole.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True\nConsole.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False\n\n"
}
]
},
{
"categoryName": "List Utilities",
"snippets": [
Expand All @@ -20,6 +113,19 @@
{
"categoryName": "String Utilities",
"snippets": [
{
"title": "Hello, World!",
"description": "Makes the first letter of a string uppercase.",
"author": "chaitanya-jvnm",
"tags": [
"c#",
"string",
"capitalize",
"utility"
],
"contributors": [],
"code": "/// <summary>\n/// Capitalize the first character of the string\n/// <summary>\npublic static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n//Example\nstring example = \"hello\";\nstring captializedExample = example.Capitalize();\nConsole.WriteLine(captializedExample); // prints \"Hello\"\n"
},
{
"title": "Truncate a String",
"description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",
Expand Down
14 changes: 14 additions & 0 deletions snippets/csharp/basics/hello-world.md
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!");
}
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/guid-utilities/generate-guid.md
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();
}
```
12 changes: 12 additions & 0 deletions snippets/csharp/guid-utilities/validate-guid.md
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 _);
}
```
22 changes: 22 additions & 0 deletions snippets/csharp/jwt-utilities/decode-jwt.md
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}
```
15 changes: 15 additions & 0 deletions snippets/csharp/jwt-utilities/generate-jwt.md
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);
}
```
35 changes: 35 additions & 0 deletions snippets/csharp/jwt-utilities/validate-jwt.md
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 snippets/csharp/string-utilities/capitalize-first-letter.md
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"
```

0 comments on commit 641f0ee

Please sign in to comment.