diff options
author | crupest <crupest@outlook.com> | 2020-01-30 20:26:52 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-01-30 20:26:52 +0800 |
commit | 52acf41e331ddbd66befed4692c804b754ba7d5c (patch) | |
tree | 538ceea06640f501d2a950cac813c10561036e4d /Timeline/Controllers/ControllerAuthExtensions.cs | |
parent | abde51b167f17301c25e32ae247e7909c0dc9367 (diff) | |
download | timeline-52acf41e331ddbd66befed4692c804b754ba7d5c.tar.gz timeline-52acf41e331ddbd66befed4692c804b754ba7d5c.tar.bz2 timeline-52acf41e331ddbd66befed4692c804b754ba7d5c.zip |
...
Diffstat (limited to 'Timeline/Controllers/ControllerAuthExtensions.cs')
-rw-r--r-- | Timeline/Controllers/ControllerAuthExtensions.cs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Timeline/Controllers/ControllerAuthExtensions.cs b/Timeline/Controllers/ControllerAuthExtensions.cs index 81fd2428..90da8a93 100644 --- a/Timeline/Controllers/ControllerAuthExtensions.cs +++ b/Timeline/Controllers/ControllerAuthExtensions.cs @@ -26,5 +26,20 @@ namespace Timeline.Controllers throw new InvalidOperationException("Failed to get user id because NameIdentifier claim is not a number.");
}
+
+ public static long? GetOptionalUserId(this ControllerBase controller)
+ {
+ if (controller.User == null)
+ return null;
+
+ var claim = controller.User.FindFirst(ClaimTypes.NameIdentifier);
+ if (claim == null)
+ throw new InvalidOperationException("Failed to get user id because User has no NameIdentifier claim.");
+
+ if (long.TryParse(claim.Value, out var value))
+ return value;
+
+ throw new InvalidOperationException("Failed to get user id because NameIdentifier claim is not a number.");
+ }
}
}
|