|
7 | 7 | "description": "Prints Hello, World! to the terminal.",
|
8 | 8 | "author": "chaitanya-jvnm",
|
9 | 9 | "tags": [
|
10 |
| - "c#", |
11 | 10 | "printing",
|
12 |
| - "hello-world", |
13 |
| - "utility" |
| 11 | + "hello-world" |
14 | 12 | ],
|
15 | 13 | "contributors": [],
|
16 | 14 | "code": "public class Program {\n public static void Main(string[] args) {\n System.Console.WriteLine(\"Hello, World!\");\n }\n}\n"
|
|
25 | 23 | "description": "Generates a new GUID",
|
26 | 24 | "author": "chaitanya-jvnm",
|
27 | 25 | "tags": [
|
28 |
| - "c#", |
29 | 26 | "guid",
|
30 |
| - "generate", |
31 |
| - "utility" |
| 27 | + "generate" |
32 | 28 | ],
|
33 | 29 | "contributors": [],
|
34 |
| - "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n" |
| 30 | + "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n\n// Usage:\nGenerateGuid(); // Returns: 1c4c38d8-64e4-431b-884a-c6eec2ab02cd (Uuid is random)\n" |
35 | 31 | },
|
36 | 32 | {
|
37 | 33 | "title": "Validate GUID",
|
38 | 34 | "description": "Checks if a string is a valid GUID.",
|
39 | 35 | "author": "chaitanya-jvnm",
|
40 | 36 | "tags": [
|
41 |
| - "c#", |
42 | 37 | "guid",
|
43 |
| - "validate", |
44 |
| - "utility" |
| 38 | + "validate" |
45 | 39 | ],
|
46 | 40 | "contributors": [],
|
47 |
| - "code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n" |
| 41 | + "code": "public static bool IsGuid(string str) {\n return Guid.TryParse(str, out _);\n}\n\n// Usage:\nIsGuid(\"1c4c38d8-64e4-431b-884a-c6eec2ab02cd\"); // Returns: true\nIsGuid(\"quicksnip\"); // Returns: false\n" |
48 | 42 | }
|
49 | 43 | ]
|
50 | 44 | },
|
|
56 | 50 | "description": "Decodes a JWT.",
|
57 | 51 | "author": "chaitanya-jvnm",
|
58 | 52 | "tags": [
|
59 |
| - "c#", |
60 | 53 | "jwt",
|
61 |
| - "decode", |
62 |
| - "utility" |
| 54 | + "decode" |
63 | 55 | ],
|
64 | 56 | "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": "Generate JWT", |
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" |
| 57 | + "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n\n// Usage:\nstring token = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nDecodeJwt(token); // Returns: \"{\\\"alg\\\":\\\"HS256\\\",\\\"typ\\\":\\\"JWT\\\"}.{\\\"sub\\\":\\\"1234567890\\\",\\\"name\\\":\\\"John Doe\\\",\\\"iat\\\":1516239022}\"\n" |
79 | 58 | },
|
80 | 59 | {
|
81 | 60 | "title": "Validate JWT",
|
82 | 61 | "description": "Validates a JWT.",
|
83 | 62 | "author": "chaitanya-jvnm",
|
84 | 63 | "tags": [
|
85 |
| - "c#", |
86 | 64 | "jwt",
|
87 |
| - "validate", |
88 |
| - "utility" |
| 65 | + "validate" |
89 | 66 | ],
|
90 | 67 | "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" |
| 68 | + "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// Usage:\nstring JWT = \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c\";\nstring correctSecret = \"your-256-bit-secret\";\nstring wrongSecret = \"this-is-not-the-right-secret\";\n\nValidateJwt(JWT, correctSecret); // Returns: true\nValidateJwt(JWT, wrongSecret); // Returns: false\n" |
92 | 69 | }
|
93 | 70 | ]
|
94 | 71 | },
|
95 | 72 | {
|
96 | 73 | "categoryName": "List Utilities",
|
97 | 74 | "snippets": [
|
98 | 75 | {
|
99 |
| - "title": "Swap two items at determined indexes", |
| 76 | + "title": "Swap items at index", |
100 | 77 | "description": "Swaps two items at determined indexes",
|
101 | 78 | "author": "omegaleo",
|
102 | 79 | "tags": [
|
103 |
| - "csharp", |
104 |
| - "c#", |
105 | 80 | "list",
|
106 |
| - "utility" |
| 81 | + "swapping" |
107 | 82 | ],
|
108 | 83 | "contributors": [],
|
109 |
| - "code": "/// <summary>\n/// Swaps the position of 2 elements inside of a List\n/// </summary>\n/// <returns>List with swapped elements</returns>\npublic static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nConsole.WriteLine(list[0]); // Outputs: Test\nConsole.WriteLine(list[1]); // Outputs: Test2\n\nlist = list.Swap(0, 1).ToList();\n\nConsole.WriteLine(list[0]); // Outputs: Test2\nConsole.WriteLine(list[1]); // Outputs: Test\n" |
| 84 | + "code": "public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)\n{\n (list[indexA], list[indexB]) = (list[indexB], list[indexA]);\n return list;\n}\n\nvar list = new List<string>() {\"Test\", \"Test2\"};\n\nlist.Swap(0, 1); // Swaps \"Test\" and \"Test2\" in place\n" |
110 | 85 | }
|
111 | 86 | ]
|
112 | 87 | },
|
|
118 | 93 | "description": "Makes the first letter of a string uppercase.",
|
119 | 94 | "author": "chaitanya-jvnm",
|
120 | 95 | "tags": [
|
121 |
| - "c#", |
122 | 96 | "string",
|
123 |
| - "capitalize", |
124 |
| - "utility" |
| 97 | + "capitalize" |
125 | 98 | ],
|
126 | 99 | "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" |
| 100 | + "code": "public static string Capitalize(this string str) {\n return str.Substring(0, 1).ToUpper() + str.Substring(1);\n}\n\n// Usage:\n\"quicksnip\".Capitalize(); // Returns: \"Quicksnip\"\n" |
128 | 101 | },
|
129 | 102 | {
|
130 |
| - "title": "Truncate a String", |
| 103 | + "title": "Truncate String", |
131 | 104 | "description": "Cut off a string once it reaches a determined amount of characters and add '...' to the end of the string",
|
132 | 105 | "author": "omegaleo",
|
133 | 106 | "tags": [
|
134 |
| - "csharp", |
135 |
| - "c#", |
136 |
| - "list", |
137 |
| - "utility" |
| 107 | + "string", |
| 108 | + "truncate" |
138 | 109 | ],
|
139 | 110 | "contributors": [],
|
140 |
| - "code": "/// <summary>\n/// Cut off a string once it reaches a <paramref name=\"maxChars\"/> amount of characters and add '...' to the end of the string\n/// </summary>\npublic static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\nvar str = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tristique rhoncus bibendum. Vivamus laoreet tortor vel neque lacinia, nec rhoncus ligula pellentesque. Nullam eu ornare nibh. Donec tincidunt viverra nulla.\";\n\nConsole.WriteLine(str); // Outputs the full string\nConsole.WriteLine(str.Truncate(5)); // Outputs Lorem...\n" |
| 111 | + "code": "public static string Truncate(this string value, int maxChars)\n{\n return value.Length <= maxChars ? value : value.Substring(0, maxChars) + \"...\";\n}\n\n// Usage:\n\"Quicksnip\".Truncate(5); // Returns: \"Quick...\"\n" |
141 | 112 | }
|
142 | 113 | ]
|
143 | 114 | }
|
|
0 commit comments