diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2020-08-01 20:14:34 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2020-08-01 20:14:34 +0200 |
| commit | a800b3b9f18ae3e8ab030c30c5d7b6504f2a5ebb (patch) | |
| tree | 68ffcdb7a2b42418ff1c4818d0b2cd5af41d5fa2 /src/server/Utilities/ExtensionMethods.cs | |
| download | dough-a800b3b9f18ae3e8ab030c30c5d7b6504f2a5ebb.tar.xz dough-a800b3b9f18ae3e8ab030c30c5d7b6504f2a5ebb.zip | |
Initial commit
Diffstat (limited to 'src/server/Utilities/ExtensionMethods.cs')
| -rw-r--r-- | src/server/Utilities/ExtensionMethods.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/server/Utilities/ExtensionMethods.cs b/src/server/Utilities/ExtensionMethods.cs new file mode 100644 index 0000000..d6bce94 --- /dev/null +++ b/src/server/Utilities/ExtensionMethods.cs @@ -0,0 +1,54 @@ +using System; +using System.Linq; +using System.Security.Claims; +using System.Security.Principal; + +namespace Dough.Utilities +{ + public static class ExtentionMethods + { + public static Guid ToGuidOrDefault(this string value) + { + if (value.IsMissing()) return default; + try + { + return new Guid(value); + } + catch (Exception e) + { + Console.WriteLine(e); + return default; + } + } + + public static DateTime ToDateTimeOrDefault(this string value) + { + if (value.IsMissing()) return default; + try + { + return DateTime.Parse(value); + } + catch (Exception e) + { + Console.WriteLine(e); + return default; + } + } + + public static bool IsMissing(this string value) => string.IsNullOrWhiteSpace(value); + public static bool IsPresent(this string value) => !value.IsMissing(); + + public static bool IsValidUrl(this string value) + { + return Uri.TryCreate(value, UriKind.Absolute, out var uriResult) + && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); + } + + public static string GetClaimValueOrDefault(this IPrincipal currentPrincipal, string claimName) + { + var identity = currentPrincipal.Identity as ClaimsIdentity; + var claim = identity?.Claims.SingleOrDefault(c => c.Type == claimName); + return claim?.Value; + } + } +} |
