|
21 | 21 | "categoryName": "Guid Utilities",
|
22 | 22 | "snippets": [
|
23 | 23 | {
|
24 |
| - "title": "Hello, World!", |
| 24 | + "title": "Generate GUID", |
25 | 25 | "description": "Generates a new GUID",
|
26 | 26 | "author": "chaitanya-jvnm",
|
27 | 27 | "tags": [
|
|
34 | 34 | "code": "public static string GenerateGuid() {\n return Guid.NewGuid().ToString();\n}\n"
|
35 | 35 | },
|
36 | 36 | {
|
37 |
| - "title": "Hello, World!", |
38 |
| - "description": "Converts a GUID to a byte array.", |
39 |
| - "author": "chaitanya-jvnm", |
40 |
| - "tags": [ |
41 |
| - "c#", |
42 |
| - "guid", |
43 |
| - "byte-array", |
44 |
| - "utility" |
45 |
| - ], |
46 |
| - "contributors": [], |
47 |
| - "code": "public static byte[] GuidToByteArray(string guid) {\n return new Guid(guid).ToByteArray();\n}\n" |
48 |
| - }, |
49 |
| - { |
50 |
| - "title": "Hello, World!", |
| 37 | + "title": "Validate GUID", |
51 | 38 | "description": "Checks if a string is a valid GUID.",
|
52 | 39 | "author": "chaitanya-jvnm",
|
53 | 40 | "tags": [
|
|
65 | 52 | "categoryName": "Jwt Utilities",
|
66 | 53 | "snippets": [
|
67 | 54 | {
|
68 |
| - "title": "Hello, World!", |
| 55 | + "title": "Decode JWT", |
69 | 56 | "description": "Decodes a JWT.",
|
70 | 57 | "author": "chaitanya-jvnm",
|
71 | 58 | "tags": [
|
|
75 | 62 | "utility"
|
76 | 63 | ],
|
77 | 64 | "contributors": [],
|
78 |
| - "code": "public static string DecodeJwt(string token) {\n return new JwtSecurityTokenHandler().ReadJwtToken(token).ToString();\n}\n" |
| 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" |
79 | 66 | },
|
80 | 67 | {
|
81 |
| - "title": "Hello, World!", |
| 68 | + "title": "Generate JWT", |
82 | 69 | "description": "Generates a new JWT.",
|
83 | 70 | "author": "chaitanya-jvnm",
|
84 | 71 | "tags": [
|
|
91 | 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"
|
92 | 79 | },
|
93 | 80 | {
|
94 |
| - "title": "Hello, World!", |
| 81 | + "title": "Validate JWT", |
95 | 82 | "description": "Validates a JWT.",
|
96 | 83 | "author": "chaitanya-jvnm",
|
97 | 84 | "tags": [
|
|
101 | 88 | "utility"
|
102 | 89 | ],
|
103 | 90 | "contributors": [],
|
104 |
| - "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" |
| 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" |
105 | 92 | }
|
106 | 93 | ]
|
107 | 94 | },
|
|
127 | 114 | "categoryName": "String Utilities",
|
128 | 115 | "snippets": [
|
129 | 116 | {
|
130 |
| - "title": "Hello, World!", |
| 117 | + "title": "Capitalize first letter", |
131 | 118 | "description": "Makes the first letter of a string uppercase.",
|
132 | 119 | "author": "chaitanya-jvnm",
|
133 | 120 | "tags": [
|
|
137 | 124 | "utility"
|
138 | 125 | ],
|
139 | 126 | "contributors": [],
|
140 |
| - "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" |
141 |
| - }, |
142 |
| - { |
143 |
| - "title": "Hello, World!", |
144 |
| - "description": "Splits a string by a delimiter.", |
145 |
| - "author": "chaitanya-jvnm", |
146 |
| - "tags": [ |
147 |
| - "c#", |
148 |
| - "string", |
149 |
| - "split", |
150 |
| - "utility" |
151 |
| - ], |
152 |
| - "contributors": [], |
153 |
| - "code": "public static string[] SplitString(string str, string delimiter) {\n return str.Split(delimiter);\n}\n" |
| 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" |
154 | 128 | },
|
155 | 129 | {
|
156 | 130 | "title": "Truncate a String",
|
|
0 commit comments