private static bool ValidateToken(string token, out string username)
        {
            username = null;

            var simplePrinciple = JwtTokenManager.GetPrincipal(token);

            if (!(simplePrinciple?.Identity is ClaimsIdentity identity))
            {
                return(false);
            }

            if (!identity.IsAuthenticated)
            {
                return(false);
            }

            var usernameClaim = identity.FindFirst(ClaimTypes.Name);

            username = usernameClaim?.Value;

            if (string.IsNullOrEmpty(username))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public string Protect(AuthenticationTicket data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var issued  = data.Properties.IssuedUtc;
            var expires = data.Properties.ExpiresUtc;

            return(JwtTokenManager.GenerateToken(_issuer, data.Identity.Claims, expires.Value.UtcDateTime, issued.Value.UtcDateTime));
        }