コード例 #1
0
        public IActionResult Login([FromBody] UserInfo user)
        {
            if (user == null)
            {
                return(BadRequest("Invalid request"));
            }
            var userInfoDataOperations = new UserInfoDBOperations(_context);

            if (userInfoDataOperations.checkIfUserExists(user))
            {
                var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
                var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

                var token = new JwtSecurityToken(_config["Jwt:Issuer"],
                                                 _config["Jwt:Issuer"],
                                                 null,
                                                 expires: DateTime.Now.AddMinutes(60),
                                                 signingCredentials: credentials);

                var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
                return(Ok(new { Token = tokenString }));
            }
            else
            {
                return(Unauthorized());
            }
        }
コード例 #2
0
 public IActionResult SignUp([FromBody] UserInfo user)
 {
     try
     {
         if (user == null)
         {
             return(BadRequest("Invalid request"));
         }
         if (!(user.UserEmailId?.Length > 0) || !(user.Password?.Length > 0))
         {
             throw new Exception("Email address and password are mandatory");
         }
         if (!RegExUtil.IsMatch(user.UserEmailId, RegExUtil.EMAIL_ID_FORMAT))
         {
             throw new Exception("Invalid Email address");
         }
         var userInfoDataOperations = new UserInfoDBOperations(_context);
         if (userInfoDataOperations.checkIfUserExists(user))
         {
             throw new Exception("User already exists");
         }
         else
         {
             return(Ok(userInfoDataOperations.SaveUserInfo(user)));
         }
     }
     catch (Exception ex)
     {
         var res = new CustomHttpErrorResponse()
         {
             StatusCode   = HttpStatusCode.ExpectationFailed,
             ErrorMessage = ex.Message
         };
         return(Ok(res));
     }
 }