Ejemplo n.º 1
0
        public CustomerCredential GetCustomerCredential(string token = "")
        {
            if (string.IsNullOrEmpty(token))
            {
                token = GetAuthToken();
            }

            if (string.IsNullOrEmpty(token))
            {
                return(null);
            }

            var user = TokenManager.GetPrincipal(token);

            if (user == null)
            {
                return(null);
            }

            var result = new CustomerCredential();

            result.Email = TokenManager.GetClaim(ClaimStore.Email, token);
            result.Name  = TokenManager.GetClaim(ClaimStore.Name, token);



            return(result);
        }
Ejemplo n.º 2
0
        public static string GenerateToken(CustomerCredential user, DateTime expireDate)
        {
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.Email),
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", user.Email),
                new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Email.ToString()),
                new Claim(ClaimStore.Email.ToString(), user.Email),
                new Claim(ClaimStore.Name.ToString(), user.Name),
            };

            var key     = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppConfigUtilities.GetAppConfig <string>("JwtKey")));
            var creds   = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var expires = DateTime.Now.AddDays(Convert.ToDouble("8"));

            var token = new JwtSecurityToken(
                issuer: AppConfigUtilities.GetAppConfig <string>("JwtIssuer"),
                audience: AppConfigUtilities.GetAppConfig <string>("JwtAudience"),
                claims: claims,
                expires: expires,
                signingCredentials: creds
                );

            string t = new JwtSecurityTokenHandler().WriteToken(token);


            return(t);
        }