From 038e8dcf461d4d4ebd51c8fdf7680497869f691c Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Jan 2020 00:10:23 +0800 Subject: ... --- Timeline/Controllers/ControllerAuthExtensions.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'Timeline/Controllers/ControllerAuthExtensions.cs') 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); } } } -- cgit v1.2.3