Ejemplo n.º 1
0
 public JwtToken GenerateSecurityToken(User user, Guid?instanceOverride = null)
 {
     return(new JwtTokenBuilder()
            .AddSecurityKey(JwtSecurityKey.Create(Settings.TokenKey))
            .AddSubject(user.UserName)
            .AddIssuer("HotelApi")
            .AddAudience("HotelApi")
            .AddClaim("UserId", user.Id.ToString())
            .AddClaim("UserFullName", FullNameFormatter.Format(user.FirstName, user.LastName, user.MiddleName))
            .AddClaim("InstanceId", (instanceOverride == null
             ? user.InstanceId
             : instanceOverride).ToString())
            .AddClaim(ClaimTypes.Role, user.Role.Identifier)
            .AddExpiry(ExpirationInMinutes)
            .Build());
 }
Ejemplo n.º 2
0
        public JwtToken RefreshSecurityToken(JwtSecurityToken existingToken)
        {
            var userId     = existingToken.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value;
            var instanceId = existingToken.Claims.FirstOrDefault(c => c.Type == "InstanceId")?.Value;
            var fullName   = existingToken.Claims.FirstOrDefault(c => c.Type == "UserFullName")?.Value;
            var role       = existingToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;

            return(new JwtTokenBuilder()
                   .AddSecurityKey(JwtSecurityKey.Create(Settings.TokenKey))
                   .AddSubject(existingToken.Subject)
                   .AddIssuer("HotelApi")
                   .AddAudience("HotelApi")
                   .AddClaim("UserId", userId)
                   .AddClaim("UserFullName", fullName ?? string.Empty)
                   .AddClaim("InstanceId", instanceId)
                   .AddClaim(ClaimTypes.Role, role)
                   .AddExpiry(ExpirationInMinutes)
                   .Build());
        }