aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Auth/MyAuthenticationHandler.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2020-02-01 00:26:35 +0800
committerGitHub <noreply@github.com>2020-02-01 00:26:35 +0800
commit7b962cd876719fb871569ba3c97fb5545721a3f8 (patch)
treef02f8d57440c777d4732bc4439f82e8b25c6732c /Timeline/Auth/MyAuthenticationHandler.cs
parent289c7e1fada1f4dae6ce5e421e997ebddd55c2df (diff)
parentbcb0a2361467614531a337282da1fd23996924f1 (diff)
downloadtimeline-7b962cd876719fb871569ba3c97fb5545721a3f8.tar.gz
timeline-7b962cd876719fb871569ba3c97fb5545721a3f8.tar.bz2
timeline-7b962cd876719fb871569ba3c97fb5545721a3f8.zip
Merge pull request #56 from crupest/dev
Refactor API to be RESTful.
Diffstat (limited to 'Timeline/Auth/MyAuthenticationHandler.cs')
-rw-r--r--Timeline/Auth/MyAuthenticationHandler.cs13
1 files changed, 7 insertions, 6 deletions
diff --git a/Timeline/Auth/MyAuthenticationHandler.cs b/Timeline/Auth/MyAuthenticationHandler.cs
index f5dcd697..3c97c329 100644
--- a/Timeline/Auth/MyAuthenticationHandler.cs
+++ b/Timeline/Auth/MyAuthenticationHandler.cs
@@ -3,11 +3,11 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using System;
+using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
-using Timeline.Models;
using Timeline.Services;
using static Timeline.Resources.Authentication.AuthHandler;
@@ -30,13 +30,13 @@ namespace Timeline.Auth
public class MyAuthenticationHandler : AuthenticationHandler<MyAuthenticationOptions>
{
private readonly ILogger<MyAuthenticationHandler> _logger;
- private readonly IUserService _userService;
+ private readonly IUserTokenManager _userTokenManager;
- public MyAuthenticationHandler(IOptionsMonitor<MyAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserService userService)
+ public MyAuthenticationHandler(IOptionsMonitor<MyAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserTokenManager userTokenManager)
: base(options, logger, encoder, clock)
{
_logger = logger.CreateLogger<MyAuthenticationHandler>();
- _userService = userService;
+ _userTokenManager = userTokenManager;
}
// return null if no token is found
@@ -78,11 +78,12 @@ namespace Timeline.Auth
try
{
- var userInfo = await _userService.VerifyToken(token);
+ var userInfo = await _userTokenManager.VerifyToken(token);
var identity = new ClaimsIdentity(AuthenticationConstants.Scheme);
+ identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userInfo.Id!.Value.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64));
identity.AddClaim(new Claim(identity.NameClaimType, userInfo.Username, ClaimValueTypes.String));
- identity.AddClaims(UserRoleConvert.ToArray(userInfo.Administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String)));
+ identity.AddClaims(UserRoleConvert.ToArray(userInfo.Administrator!.Value).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String)));
var principal = new ClaimsPrincipal();
principal.AddIdentity(identity);