Skip to content

Commit 641f0ee

Browse files
Merge pull request #121 from chaitanya-jvnm/create-csharp-snippets
feat: Adding CSharp Snippets
2 parents e74409d + 55bd1e5 commit 641f0ee

File tree

8 files changed

+236
-0
lines changed

8 files changed

+236
-0
lines changed

public/consolidated/csharp.json

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,97 @@
11
[
2+
{
3+
"categoryName": "Basics",
4+
"snippets": [
5+
{
6+
"title": "Hello, World!",
7+
"description": "Prints Hello, World! to the terminal.",
8+
"author": "chaitanya-jvnm",
9+
"tags": [
10+
"c#",
11+
"printing",
12+
"hello-world",
13+
"utility"
14+
],
15+
"contributors": [],
16+
"code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n"
17+
}
18+
]
19+
},
20+
{
21+
"categoryName": "Guid Utilities",
22+
"snippets": [
23+
{
24+
"title": "Hello, World!",
25+
"description": "Generates a new GUID",
26+
"author": "chaitanya-jvnm",
27+
"tags": [
28+
"c#",
29+
"guid",
30+
"generate",
31+
"utility"
32+
],
33+
"contributors": [],
34+
"code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n"
35+
},
36+
{
37+
"title": "Hello, World!",
38+
"description": "Checks if a string is a valid GUID.",
39+
"author": "chaitanya-jvnm",
40+
"tags": [
41+
"c#",
42+
"guid",
43+
"validate",
44+
"utility"
45+
],
46+
"contributors": [],
47+
"code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n"
48+
}
49+
]
50+
},
51+
{
52+
"categoryName": "Jwt Utilities",
53+
"snippets": [
54+
{
55+
"title": "Hello, World!",
56+
"description": "Decodes a JWT.",
57+
"author": "chaitanya-jvnm",
58+
"tags": [
59+
"c#",
60+
"jwt",
61+
"decode",
62+
"utility"
63+
],
64+
"contributors": [],
65+
"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"
66+
},
67+
{
68+
"title": "Hello, World!",
69+
"description": "Generates a new JWT.",
70+
"author": "chaitanya-jvnm",
71+
"tags": [
72+
"c#",
73+
"jwt",
74+
"generate",
75+
"utility"
76+
],
77+
"contributors": [],
78+
"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"
79+
},
80+
{
81+
"title": "Hello, World!",
82+
"description": "Validates a JWT.",
83+
"author": "chaitanya-jvnm",
84+
"tags": [
85+
"c#",
86+
"jwt",
87+
"validate",
88+
"utility"
89+
],
90+
"contributors": [],
91+
"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"
92+
}
93+
]
94+
},
295
{
396
"categoryName": "List Utilities",
497
"snippets": [
@@ -20,6 +113,19 @@
20113
{
21114
"categoryName": "String Utilities",
22115
"snippets": [
116+
{
117+
"title": "Hello, World!",
118+
"description": "Makes the first letter of a string uppercase.",
119+
"author": "chaitanya-jvnm",
120+
"tags": [
121+
"c#",
122+
"string",
123+
"capitalize",
124+
"utility"
125+
],
126+
"contributors": [],
127+
"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"
128+
},
23129
{
24130
"title": "Truncate a String",
25131
"description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",

snippets/csharp/basics/hello-world.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Hello, World!
3+
description: Prints Hello, World! to the terminal.
4+
author: chaitanya-jvnm
5+
tags: c#,printing,hello-world,utility
6+
---
7+
8+
```c#
9+
public class Program {
10+
public static void Main(string[] args) {
11+
System.Console.WriteLine("Hello, World!");
12+
}
13+
}
14+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Generates a new GUID
4+
author: chaitanya-jvnm
5+
tags: c#,guid,generate,utility
6+
---
7+
8+
```c#
9+
public static string GenerateGuid() {
10+
return Guid.NewGuid().ToString();
11+
}
12+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Hello, World!
3+
description: Checks if a string is a valid GUID.
4+
author: chaitanya-jvnm
5+
tags: c#,guid,validate,utility
6+
---
7+
8+
```c#
9+
public static bool IsGuid(string str) {
10+
return Guid.TryParse(str, out _);
11+
}
12+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Hello, World!
3+
description: Decodes a JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,decode,utility
6+
---
7+
8+
```c#
9+
/// <summary>
10+
/// Decodes the JWT
11+
/// <summary>
12+
public static string DecodeJwt(string token) {
13+
return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();
14+
}
15+
16+
//Example
17+
string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
18+
19+
string decodedJwt = DecodeJwt(token);
20+
21+
Console.WriteLine(decodedJwt); //Prints {"alg":"HS256","typ":"JWT"}.{"sub":"1234567890","name":"John Doe","iat":1516239022}
22+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Hello, World!
3+
description: Generates a new JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,generate,utility
6+
---
7+
8+
```c#
9+
public static string GenerateJwt(string secret, string issuer, string audience, int expirationMinutes) {
10+
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret));
11+
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
12+
var token = new JwtSecurityToken(issuer, audience, null, expires: DateTime.UtcNow.AddMinutes(expirationMinutes), signingCredentials: credentials);
13+
return new JwtSecurityTokenHandler().WriteToken(token);
14+
}
15+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Hello, World!
3+
description: Validates a JWT.
4+
author: chaitanya-jvnm
5+
tags: c#,jwt,validate,utility
6+
---
7+
8+
```c#
9+
public static bool ValidateJwt(string token, string secret) {
10+
var tokenHandler = new JwtSecurityTokenHandler();
11+
var validationParameters = new TokenValidationParameters {
12+
ValidateIssuerSigningKey = true,
13+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
14+
ValidateIssuer = false,
15+
ValidateAudience = false
16+
};
17+
try {
18+
tokenHandler.ValidateToken(token, validationParameters, out _);
19+
return true;
20+
}
21+
catch {
22+
return false
23+
}
24+
}
25+
26+
//Example
27+
string JWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
28+
29+
string correctSecret = "your-256-bit-secret";
30+
string wrongSecret = "this-is-not-the-right-secret";
31+
32+
Console.WriteLine(ValidateJwt(JWT, correctSecret)) // returns True
33+
Console.WriteLine(ValidateJwt(JWT, wrongSecret)) // returns False
34+
35+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Hello, World!
3+
description: Makes the first letter of a string uppercase.
4+
author: chaitanya-jvnm
5+
tags: c#,string,capitalize,utility
6+
---
7+
8+
```c#
9+
/// <summary>
10+
/// Capitalize the first character of the string
11+
/// <summary>
12+
public static string Capitalize(this string str) {
13+
return str.Substring(0, 1).ToUpper() + str.Substring(1);
14+
}
15+
16+
//Example
17+
string example = "hello";
18+
string captializedExample = example.Capitalize();
19+
Console.WriteLine(captializedExample); // prints "Hello"
20+
```

0 commit comments

Comments
 (0)