public HttpResponseMessage Register(RegisterViewModel model) { if (model.Password != model.ConfirmPassword) { return new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "The password and confirmation password do not match." }; } ApplicationUser user = new ApplicationUser { UserName = model.Name, Email = model.Email }; IdentityResult result = UserManager.CreateAsync(user, model.Password).Result; if (result.Succeeded) { SignInManager.SignIn(user, false, false); return new HttpResponseMessage(HttpStatusCode.OK) { ReasonPhrase = "Successful registration.", Content = new ObjectContent<ApplicationUser>(user, new JsonMediaTypeFormatter()) }; } return new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = result.Errors.First() }; }
//private User GetUser(string fn, string ln, string pass) //{ // IUserRepository userRepository = new UserRepository(); // return userRepository.GetUser(fn, ln, Encrypt(pass)); //} //private bool CreateUserAccount() //{ // IUserRepository userRepository = new UserRepository(); // if (userRepository.GetUser(_user.FirstName, _user.LastName, _user.PasswordHash) != null) // { // return false; // } // userRepository.Save(_user, _user.Id); // return true; //} //private string Encrypt(string password) //{ // SHA256 sha256 = SHA256.Create(); // byte[] data = Encoding.UTF8.GetBytes(password); // byte[] result = sha256.ComputeHash(data); // string hashString = result.Aggregate(string.Empty, (current, x) => current + $"{x:x2}"); // return hashString; //} private void LoginButton_OnClick(object sender, RoutedEventArgs e) { _mySqlDbDataModifier = new MySqlDbDataModifier(); //new MySqlMeasurableParameterMappingRepository(), // new MySqlPlantMappingRepository(), new MySqlPlantsAreaMappingRepository(), // new MySqlSensorMappingRepository(), new MySqlServiceScheduleMappingRepository(), new MySqlMeasuringMessageMappingRepository(), new SensorsCollection(), new PlantsAreas()); //MeasurableParameterMapping mpm = new MeasurableParameterMapping(Guid.NewGuid(), 10, 9, 11, // ParameterEnum.Temperature.ToString()); //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Save(mpm, Guid.Empty); //var mpm1 = _mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Get(mpm.Id); //mpm1.Optimal = 23; //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Save(mpm1, Guid.Empty); //_mySqlDbDataModifier.SqlMeasurableParameterMappingRepository.Delete(mpm1.Id); //PlantMapping plantMapping = new PlantMapping(Guid.NewGuid(), new Guid("a2948b2a-921f-4c7c-806d-a5724e7302cb"), // new Guid("3555C74B-E887-4685-A627-005017305139"), new Guid("FDCF0612-1077-45D6-B294-272504C4E51E"), // new Guid("F8867AAB-1B2C-4629-BB51-04E61B714222"), // "Apple", null); //_mySqlDbDataModifier.SqlPlantMappingRepository.Save(plantMapping, Guid.Empty); //var mpm1 = _mySqlDbDataModifier.SqlPlantMappingRepository.Get(plantMapping.Id); //mpm1.Name = "Pear"; //_mySqlDbDataModifier.SqlPlantMappingRepository.Save(mpm1, Guid.Empty); //_mySqlDbDataModifier.SqlPlantMappingRepository.Delete(mpm1.Id); //PlantsAreaMapping mpm = new PlantsAreaMapping(Guid.NewGuid(), new Guid("aafdb697-fd67-48e8-9ea8-7f2afe5989d0"), 10, new Guid("9adc5dca-94bc-4c35-afb5-42b06eedf989")); //_mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Save(mpm, Guid.Empty); //var mpm1 = _mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Get(mpm.Id); //mpm1.Number = 23; //_mySqlDbDataModifier.SqlPlantsAreaMappingRepository.Save(mpm1, Guid.Empty); //SensorMapping mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3, // new Guid("a2948b2a-921f-4c7c-806d-a5724e7302cb"), ParameterEnum.Temperature.ToString()); //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty); //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3, // new Guid("3555C74B-E887-4685-A627-005017305139"), ParameterEnum.Humidity.ToString()); //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty); //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3, // new Guid("FDCF0612-1077-45D6-B294-272504C4E51E"), ParameterEnum.Nutrient.ToString()); //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty); //mpm = new SensorMapping(Guid.NewGuid(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 3, // new Guid("F8867AAB-1B2C-4629-BB51-04E61B714222"), ParameterEnum.SoilPh.ToString()); //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm, Guid.Empty); //var mpm1 = _mySqlDbDataModifier.SqlSensorMappingRepository.Get(new Guid("74e4da76-a799-48b4-b75e-65e5f3fc2cd8")); //mpm1.MeasuringTimeout = 25; //_mySqlDbDataModifier.SqlSensorMappingRepository.Save(mpm1, Guid.Empty); //ServiceScheduleMapping mpm = new ServiceScheduleMapping(Guid.NewGuid(), // new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Watering.ToString(), 3, 2, // "a2948b2a-921f-4c7c-806d-a5724e7302cb,3555C74B-E887-4685-A627-005017305139"); //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty); //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("2e664c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Nutrienting.ToString(), 3, 2, // "F8867AAB-1B2C-4629-BB51-04E61B714222,FDCF0612-1077-45D6-B294-272504C4E51E"); //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty); //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("7be64c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Warming.ToString(), 3, 2, // "F8867AAB-1B2C-4629-BB51-04E61B714222"); //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty); //mpm = new ServiceScheduleMapping(Guid.NewGuid(), new Guid("9ce64c4e-8300-4a2c-a7d5-3829005b1d7e"), ServiceStateEnum.Cooling.ToString(), 3, 2, // "F8867AAB-1B2C-4629-BB51-04E61B714222"); //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm, Guid.Empty); //var mpm1 = _mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Get(new Guid("0824a75a-f91e-4f57-84a8-1f9bf45a995f")); //mpm1.ServicingPauseSpan = 5; //_mySqlDbDataModifier.SqlServiceScheduleMappingRepository.Save(mpm1, Guid.Empty); //var mm = new MeasuringMessageMapping(Guid.NewGuid(), DateTime.Now, MessageTypeEnum.UsualInfo.ToString(), // ParameterEnum.Nutrient.ToString(), new Guid("48664c4e-8300-4a2c-a7d5-3829005b1d7e"), 12.66); //_mySqlDbDataModifier.SqlMeasuringMessageMappingRepository.Save(mm, Guid.Empty); //_mySqlDbDataModifier.SqlMeasuringMessageMappingRepository.Delete(mm.Id); //IUserRepository userRepository = new UserRepository(); //foreach (var user in userRepository.GetAll()) //{ // _mongoDbAccessor = new MongoDbAccessor(); // _mongoDbAccessor.AddMongoUser(new MongoUser(user)); //} Logginglabel.Content = @"You are being logged in. Please, wait..."; LoginButton.IsEnabled = false; string firstName = FirstName.Text; string lastName = LastName.Text; string email = Email.Text; string password = Password.Password; HttpResponseMessage response; if (CreateAccount.IsChecked != null && !(bool)CreateAccount.IsChecked) { //_user = GetUser(firstName, lastName, password); //if (_user == null) //{ // Logginglabel.Content = @"User with such credentials does not exist"; // LoginButton.IsEnabled = true; // return; //} LoginViewModel loginViewModel = new LoginViewModel { Email = email, Password = password, RememberMe = true }; response = _mySqlDbDataModifier.LoginUser(loginViewModel); if (!response.IsSuccessStatusCode) { Logginglabel.Content = response.ReasonPhrase; LoginButton.IsEnabled = true; //_user = (response.Content as ObjectContent)?.Value as ApplicationUser; return; } _user = response.Content.ReadAsAsync<ApplicationUser>().Result; } else { string confirmPassword = ConfirmPassword.Password; //EmailAddressAttribute addressAttribute = new EmailAddressAttribute(); //if (!addressAttribute.IsValid(Email.Text)) //{ // Logginglabel.Content = @"Email is wrong"; // LoginButton.IsEnabled = true; // return; //} //if (password != confirmPassword) //{ // Logginglabel.Content = @"Passwords do not match"; // LoginButton.IsEnabled = true; // return; //} string username = $"{firstName} {lastName}"; RegisterViewModel registerViewModel = new RegisterViewModel { Name = username, Email = email, Password = password, ConfirmPassword = confirmPassword }; response = _mySqlDbDataModifier.RegisterUser(registerViewModel); if (!response.IsSuccessStatusCode) { Logginglabel.Content = response.ReasonPhrase; LoginButton.IsEnabled = true; //_user = (response.Content as ObjectContent)?.Value as ApplicationUser; return; } _user = response.Content.ReadAsAsync<ApplicationUser>().Result; } //for MS Sql and Mongo DBs //_user = new User(firstName, lastName, email, Encrypt(password)); //if (CreateUserAccount()) //{ // _mongoDbAccessor = new MongoDbAccessor(); // _mongoDbAccessor.AddMongoUser(new MongoUser(_user)); //} //else //{ // Logginglabel.Content = @"User with such credentials already exists"; // LoginButton.IsEnabled = true; // return; //} if (_user != null) { StartMainProcess(); LoginNameLabel.Content = $"You are logged in as {_user.UserName}"; LoginNameLabel.Background = Brushes.Wheat; } }
public HttpResponseMessage RegisterUser(RegisterViewModel registerViewModel) { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(_baseServerUr); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PostAsJsonAsync("api/identity/register", registerViewModel).Result; return response; } }