|
@@ -4,11 +4,7 @@ using Abp.UI;
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
-using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
-using System.Security.Claims;
|
|
|
|
|
-
|
|
|
|
|
using VberAdmin.Authentication.External;
|
|
using VberAdmin.Authentication.External;
|
|
|
-using VberAdmin.Authentication.JwtBearer;
|
|
|
|
|
using VberAdmin.Authorization;
|
|
using VberAdmin.Authorization;
|
|
|
using VberAdmin.Authorization.Users;
|
|
using VberAdmin.Authorization.Users;
|
|
|
using VberAdmin.Models.TokenAuth;
|
|
using VberAdmin.Models.TokenAuth;
|
|
@@ -17,6 +13,7 @@ using VberZero.AppService.Authorization;
|
|
|
using VberZero.Authorization;
|
|
using VberZero.Authorization;
|
|
|
using VberZero.Authorization.Users;
|
|
using VberZero.Authorization.Users;
|
|
|
using VberZero.BaseSystem.Users;
|
|
using VberZero.BaseSystem.Users;
|
|
|
|
|
+using VberZero.Jwt;
|
|
|
using VberZero.MultiTenancy;
|
|
using VberZero.MultiTenancy;
|
|
|
|
|
|
|
|
namespace VberAdmin.Controllers;
|
|
namespace VberAdmin.Controllers;
|
|
@@ -63,7 +60,7 @@ public class TokenAuthController : VberAdminControllerBase
|
|
|
tenancyName
|
|
tenancyName
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity));
|
|
|
|
|
|
|
+ var accessToken = JwtHelper.CreateAccessToken(loginResult.Identity, _configuration);
|
|
|
|
|
|
|
|
return new AuthenticateResultModel
|
|
return new AuthenticateResultModel
|
|
|
{
|
|
{
|
|
@@ -91,7 +88,7 @@ public class TokenAuthController : VberAdminControllerBase
|
|
|
{
|
|
{
|
|
|
case VzLoginResultType.Success:
|
|
case VzLoginResultType.Success:
|
|
|
{
|
|
{
|
|
|
- var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity));
|
|
|
|
|
|
|
+ var accessToken = JwtHelper.CreateAccessToken(loginResult.Identity, _configuration);
|
|
|
return new ExternalAuthenticateResultModel
|
|
return new ExternalAuthenticateResultModel
|
|
|
{
|
|
{
|
|
|
AccessToken = accessToken,
|
|
AccessToken = accessToken,
|
|
@@ -121,7 +118,7 @@ public class TokenAuthController : VberAdminControllerBase
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity));
|
|
|
|
|
|
|
+ var accessToken = JwtHelper.CreateAccessToken(loginResult.Identity, _configuration);
|
|
|
|
|
|
|
|
return new ExternalAuthenticateResultModel
|
|
return new ExternalAuthenticateResultModel
|
|
|
{
|
|
{
|
|
@@ -202,38 +199,6 @@ public class TokenAuthController : VberAdminControllerBase
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private string CreateAccessToken(IEnumerable<Claim> claims, TimeSpan? expiration = null)
|
|
|
|
|
- {
|
|
|
|
|
- var now = DateTime.UtcNow;
|
|
|
|
|
-
|
|
|
|
|
- var jwtSecurityToken = new JwtSecurityToken(
|
|
|
|
|
- issuer: _configuration.Issuer,
|
|
|
|
|
- audience: _configuration.Audience,
|
|
|
|
|
- claims: claims,
|
|
|
|
|
- notBefore: now,
|
|
|
|
|
- expires: now.Add(expiration ?? _configuration.Expiration),
|
|
|
|
|
- signingCredentials: _configuration.SigningCredentials
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- return new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static List<Claim> CreateJwtClaims(ClaimsIdentity identity)
|
|
|
|
|
- {
|
|
|
|
|
- var claims = identity.Claims.ToList();
|
|
|
|
|
- var nameIdClaim = claims.First(c => c.Type == ClaimTypes.NameIdentifier);
|
|
|
|
|
-
|
|
|
|
|
- // Specifically add the jti (random nonce), iat (issued timestamp), and sub (subject/user) claims.
|
|
|
|
|
- claims.AddRange(new[]
|
|
|
|
|
- {
|
|
|
|
|
- new Claim(JwtRegisteredClaimNames.Sub, nameIdClaim.Value),
|
|
|
|
|
- new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
|
|
|
|
- new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.Now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64)
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- return claims;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
private string GetEncryptedAccessToken(string accessToken)
|
|
private string GetEncryptedAccessToken(string accessToken)
|
|
|
{
|
|
{
|
|
|
return SimpleStringCipher.Instance.Encrypt(accessToken);
|
|
return SimpleStringCipher.Instance.Encrypt(accessToken);
|