public async Task <ActionResult <User> > Register(UserToSave userToSave) { try { // Validation of input Data var validation = new UserToSaveResourceValidation(); var validationResult = await validation.ValidateAsync(userToSave); if (!validationResult.IsValid) { return(BadRequest(validationResult.Errors)); } // Mapping var user = _mapperService.Map <UserToSave, User>(userToSave); // Creation var userCreated = await _userService.Create(user, userToSave.Password); // Send Token var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_config.GetValue <string>("AppSettings:Secret")); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials( new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature ) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); return(Ok( new { Id = user.Id, UserName = user.UserName, FirstName = user.FirstName, LastName = user.LastName, Token = tokenString })); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public JsonResult Post([FromBody] UserToSave user) { string conString = "Data Source=DABEER-DEVELOPE; Initial Catalog=MyLocker; user id=sa; password=admin;"; SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = conString; SqlCommand sqlCommand = new SqlCommand("INSERT INTO userSample(username,password,first_name,last_name,dob,created,modified) VALUES('" + user.userName + "',sys.fn_varbintohexstr(HASHBYTES('MD5','" + user.password + "')),'shaheer','ghofran','" + user.dob + "','" + DateTime.Now + "',NULL)", sqlConnection); sqlConnection.Open(); sqlCommand.ExecuteNonQuery(); sqlConnection.Close(); return(new JsonResult(user)); }
public JsonResult Post([FromBody] UserToSave user) { string conn = "Server=tcp:mylockersqlserver.database.windows.net,1433;Initial Catalog=MyLocker;Persist Security Info=False;User ID=sqladmin;Password=Pass1984;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"; //string conn = "Data Source=DABEER-DEVELOPE; Initial Catalog=MyLocker; User Id=sa;password=admin;"; SqlConnection sqlConnection = new SqlConnection(conn); Guid roleId = Guid.NewGuid(); Guid userId = Guid.NewGuid(); sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand("INSERT INTO user_role(id,name,description,created,modified) VALUES('" + roleId + user.roleName + "','" + user.roleDescription + "','" + DateTime.Now + "','NULL')", sqlConnection); sqlCommand.ExecuteNonQuery(); SqlCommand sqlCommand2 = new SqlCommand("INSERT INTO users(username,password_hash,dob,first_name,last_name," + "email,status,reset_token,reset_expiry,last_login,profile_pic_url,NIN,is_social_login,created,modified,id," + "role_id,guardian_id) VALUES('" + user.userName + "',sys.fn_varbintohexstr(HASHBYTES('MD5','" + user.password + "'))" + ",'" + user.dob + "','" + user.firstName + "','" + user.lastName + "','" + user.emailId + "','active','NULL','NULL'" + "'NULL','" + user.profilePicUrl + "','" + user.nationalInsNo + "','" + user.isSocialLogin + "','" + DateTime.Now + "','NULL','" + userId + "','" + roleId + "','NULL')", sqlConnection); sqlCommand2.ExecuteNonQuery(); SqlCommand sqlCommand3 = new SqlCommand("INSERT INTO user_details(user_id,country,state,city,zip_code,address_line_1," + "address_line_2,employer_name,employer_status,education,marital_status,club_name,council_no,created,modified" + "VALUES('" + userId + "','" + user.country + "','" + user.state + "','" + user.town + "','" + user.zipCode + "','" + user.addressLine1 + "','" + user.addressLine2 + "','" + user.employerName + "','" + user.employerStatus + "','" + user.education + "','" + user.MaritalStatus + "','" + user.clubName + "','" + user.countcilNo + "','" + DateTime.Now + "','NULL')", sqlConnection); sqlCommand3.ExecuteNonQuery(); sqlConnection.Close(); return(new JsonResult(user)); }