Demo: Implement Create Token

In the Conduit.Repositories Project modify AccountRepository Class

Modify the CreateUserAsync Method
  • After the CreateUser method call, add a new
  • Assign user.Token to the result of a non-existing method called CreateToken
  • Pass in the user object as a parameter
  • Let the IDE generate the method for you
Modify the CreateToken Method
  • Add the following code inside the method below:
        private string CreateToken(User user)
            var tokenHandler = new JwtSecurityTokenHandler();
            var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value);
            var tokenDescriptor = new SecurityTokenDescriptor
                Subject = new ClaimsIdentity(new Claim[]
                    new Claim(ClaimTypes.NameIdentifier, user.UserName),
                    new Claim(ClaimTypes.Name, user.Email)
                NotBefore = DateTime.Now,
                Expires = DateTime.Now.AddMinutes(20),
                SigningCredentials = new SigningCredentials(
                    new SymmetricSecurityKey(key),
            var token = tokenHandler.CreateToken(tokenDescriptor);
            return tokenHandler.WriteToken(token);
  • Resolve any missing namespaces

In the video I explain all the different pieces of the code and what they represent. You're more than welcome to follow along with the video if you would like to understand the code that creates the JSON Web Token.