Beispiel #1
0
        public IActionResult Login([FromBody] LoginParameters LoginParameters)
        {
            Users users = _mapper.Map <Users>(LoginParameters);

            try
            {
                if (LoginParameters == null)
                {
                    return(BadRequest("Specified Object is null or empty"));
                }

                var claim = new[] {
                    new Claim(JwtRegisteredClaimNames.Sub, LoginParameters.MobileNumber)
                };

                _response = _IAuthenticate.AuthenticateUsers(users);


                /*To get token first */
                var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_IConfiguration["Jwt:SigningKey"]));

                int exPiryInMinute = Convert.ToInt32(_IConfiguration["Jwt:ExpiryInMinutes"]);

                var token = new JwtSecurityToken(
                    issuer: _IConfiguration["Jwt:Site"],
                    audience:  _IConfiguration["Jwt:Site"],
                    expires: DateTime.UtcNow.AddMinutes(exPiryInMinute),
                    signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256)
                    );

                return(Ok(new {
                    token = new JwtSecurityTokenHandler().WriteToken(token),
                    expiration = token.ValidTo,
                    response = _response
                }));

                //return Ok(_response);
            }
            catch (Exception ex)
            {
                var exM = ex == null ? ex.InnerException.Message : ex.Message;
                _response.ResponseCode = "99";
                _response.ResponseText = exM;
                return(Ok(_response));
            }

            return(BadRequest());
        }