aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/ControllerAuthExtensions.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-31 00:10:23 +0800
committercrupest <crupest@outlook.com>2020-01-31 00:10:23 +0800
commit038e8dcf461d4d4ebd51c8fdf7680497869f691c (patch)
treedfd6ca1258d61ed91e59be620a39159919d07a3f /Timeline/Controllers/ControllerAuthExtensions.cs
parent5aaa4f95e6bdd46e6740c1ecbbd46bdf415eedd2 (diff)
downloadtimeline-038e8dcf461d4d4ebd51c8fdf7680497869f691c.tar.gz
timeline-038e8dcf461d4d4ebd51c8fdf7680497869f691c.tar.bz2
timeline-038e8dcf461d4d4ebd51c8fdf7680497869f691c.zip
...
Diffstat (limited to 'Timeline/Controllers/ControllerAuthExtensions.cs')
-rw-r--r--Timeline/Controllers/ControllerAuthExtensions.cs15
1 files changed, 5 insertions, 10 deletions
diff --git a/Timeline/Controllers/ControllerAuthExtensions.cs b/Timeline/Controllers/ControllerAuthExtensions.cs
index 34fd4d99..00a65454 100644
--- a/Timeline/Controllers/ControllerAuthExtensions.cs
+++ b/Timeline/Controllers/ControllerAuthExtensions.cs
@@ -1,7 +1,8 @@
using Microsoft.AspNetCore.Mvc;
+using System;
using System.Security.Claims;
using Timeline.Auth;
-using System;
+using static Timeline.Resources.Controllers.ControllerAuthExtensions;
namespace Timeline.Controllers
{
@@ -14,24 +15,18 @@ namespace Timeline.Controllers
public static long GetUserId(this ControllerBase controller)
{
- if (controller.User == null)
- throw new InvalidOperationException("Failed to get user id because User is 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.");
+ throw new InvalidOperationException(ExceptionNoUserIdentifierClaim);
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.");
+ throw new InvalidOperationException(ExceptionUserIdentifierClaimBadFormat);
}
public static long? GetOptionalUserId(this ControllerBase controller)
{
- if (controller.User == null)
- return null;
-
var claim = controller.User.FindFirst(ClaimTypes.NameIdentifier);
if (claim == null)
return null;
@@ -39,7 +34,7 @@ namespace Timeline.Controllers
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.");
+ throw new InvalidOperationException(ExceptionUserIdentifierClaimBadFormat);
}
}
}