Example #1
0
        public async Task <IActionResult> RequestTokenAsync([FromBody] TokenRequest request, [FromServices] IDapperRepository myDR)
        {
            var myLogin = await myDR.GetRecord <AppLogin>("select * from AppLogin", new AppLogin { username = request.Username, password = request.Password });

            if (myLogin != null && myLogin.id != null)
            {
                var claims = new[]
                {
                    new Claim(ClaimTypes.Name, request.Username)
                };

                var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenConfig.Value.SecretKey));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                var token = new JwtSecurityToken(
                    issuer: tokenConfig.Value.Issuer,
                    audience: tokenConfig.Value.Audience,
                    claims: claims,
                    expires: DateTime.Now.AddMinutes(30),
                    signingCredentials: creds);

                return(Ok(new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token)
                }));
            }
            return(BadRequest("Could not verify username and password"));
        }