|
1 | 1 | [
|
| 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 | + }, |
2 | 95 | {
|
3 | 96 | "categoryName": "List Utilities",
|
4 | 97 | "snippets": [
|
|
20 | 113 | {
|
21 | 114 | "categoryName": "String Utilities",
|
22 | 115 | "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 | + }, |
23 | 129 | {
|
24 | 130 | "title": "Truncate a String",
|
25 | 131 | "description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",
|
|
0 commit comments