Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] SignInModel model)
        {
            try
            {
                User user;
                var  claims = new List <Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, model.UserName),
                    new Claim("name", model.UserName),
                    //new Claim(JwtRegisteredClaimNames.Sub, model.UserName),
                    //new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                };

                var isValid = await _userService.ValidateCredentials(model.UserName, model.Password, out user);

                if (isValid)
                {
                    var issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(InMemoryConfig.SecretKey));

                    //https://stackoverflow.com/questions/49875167/jwt-error-idx10634-unable-to-create-the-signatureprovider-c-sharp
                    var creds = new SigningCredentials(issuerSigningKey, SecurityAlgorithms.HmacSha256Signature);

                    var token = new JwtSecurityToken(InMemoryConfig.Issuer, InMemoryConfig.Audience, claims,
                                                     expires: DateTime.UtcNow.AddMinutes(5), signingCredentials: creds);

                    var tokenValue = new JwtSecurityTokenHandler().WriteToken(token);
                    return(Ok(Envelop.Ok(tokenValue)));
                }
                return(BadRequest(Envelop.Error("User name or password is incorrect!")));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(BadRequest(e.Message));
            }
        }
Exemplo n.º 2
0
 protected new IActionResult Ok()
 {
     return(base.Ok(Envelop.Ok()));
 }
Exemplo n.º 3
0
 protected IActionResult Ok <T>(T result)
 {
     return(base.Ok(Envelop.Ok(result)));
 }