Exemplo n.º 1
0
        public async Task <JwtRéponse> CréeReponse(CarteUtilisateur carte)
        {
            List <Claim> claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, carte.Utilisateur.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                new Claim(JwtClaims.UserId, carte.Utilisateur.Id),
                new Claim(JwtClaims.UserName, carte.Utilisateur.UserName),
                new Claim(JwtClaims.SessionId, carte.Utilisateur.SessionId.ToString())
            };

            JwtSecurityToken jwt = new JwtSecurityToken(
                issuer: _jwtOptions.Issuer,
                audience: _jwtOptions.Audience,
                claims: claims.ToArray(),
                notBefore: _jwtOptions.NotBefore,
                expires: _jwtOptions.Expiration,
                signingCredentials: _jwtOptions.SigningCredentials);

            string jeton = new JwtSecurityTokenHandler().WriteToken(jwt);

            JwtRéponse jwtr = new JwtRéponse
            {
                Id         = carte.Utilisateur.Id,
                Jeton      = jeton,
                ExpireDans = (int)_jwtOptions.ValidFor.TotalSeconds,
            };

            return(jwtr);
        }
Exemplo n.º 2
0
        public async Task <JwtRéponse> CréeReponse(CarteUtilisateur carte)
        {
            List <Claim> claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Sub, carte.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                new Claim(JwtClaims.UserId, carte.UserId),
                new Claim(JwtClaims.UserName, carte.UserName),
                new Claim(JwtClaims.UtilisateurId, carte.Uid),
                new Claim(JwtClaims.EtatUtilisateur, carte.Etat),
                new Claim(JwtClaims.Roles, carte.RolesSérialisés),
            };

            string jeton = CréeJeton(claims);

            JwtRéponse jwtr = new JwtRéponse
            {
                Id         = carte.UserId,
                Jeton      = jeton,
                ExpireDans = (int)_jwtOptions.ValidFor.TotalSeconds,
            };

            return(jwtr);
        }