Exemplo n.º 1
0
        public HttpResponseMessage CheckEmail(string email)
        {
            int    responseVal  = 0;
            string responseText = "OK";
            bool   error        = false;

            Thread.Sleep(3000);

            #region Parameters Control

            if (string.IsNullOrWhiteSpace(email))
            {
                responseVal  = 3;
                responseText = HomeM8.GetWarningString(3).Replace("#Parametre#", nameof(email));
                error        = true;
            }

            #endregion

            #region Main Process

            if (!error)
            {
                if (!HomeM8.EmailValid(email))
                {
                    responseVal  = 3008;
                    responseText = HomeM8.GetWarningString(3008);
                    error        = true;
                }
            }

            #endregion

            return(new HttpResponseMessage()
            {
                Content = new StringContent(JsonConvert.SerializeObject(new
                {
                    responseVal = responseVal,
                    responseText = responseText
                }))
            });
        }
Exemplo n.º 2
0
        public async Task <HttpResponseMessage> Register(string username)
        {
            int    responseVal        = 0;
            string responseText       = "OK";
            bool   error              = false;
            string cipheredParameters = Request.Content.ReadAsStringAsync().Result;

            #region Method Specific Variables

            var plainParameters = new
            {
                Email       = default(string),
                Password    = default(string),
                PhoneNumber = default(string),
                NameSurname = default(string)
            };

            byte[] sharedSecret = null;

            #endregion

            #region Parameters Control

            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(cipheredParameters))
            {
                responseVal  = 1;
                responseText = HomeM8.GetWarningString(1);
                error        = true;
            }

            #endregion

            #region Main Process

            if (!error)
            {
                using (HomeM8Entities db = new HomeM8Entities())
                {
                    if (db.FromRegister.FirstOrDefault(each => each.Username == username) is FromRegister registeredUser)
                    {
                        sharedSecret = registeredUser.SharedSecret;

                        #region Decryption

                        try
                        {
                            string plainJsonString = Security.DecryptAES(registeredUser.SharedSecret, cipheredParameters);
                            try
                            {
                                plainParameters = JsonConvert.DeserializeAnonymousType(plainJsonString, plainParameters);
                            }
                            catch
                            {
                                responseVal  = 6;
                                responseText = HomeM8.GetWarningString(6);
                                error        = true;
                            }
                        }
                        catch (Exception)
                        {
                            responseVal  = 3;
                            responseText = HomeM8.GetWarningString(3).Replace("#Parametre#", nameof(cipheredParameters));
                            error        = true;
                        }

                        #endregion

                        #region Plain Parameters Control

                        if (!error)
                        {
                            if (string.IsNullOrWhiteSpace(plainParameters.Email) &&
                                string.IsNullOrWhiteSpace(plainParameters.NameSurname) &&
                                string.IsNullOrWhiteSpace(plainParameters.Password) &&
                                string.IsNullOrWhiteSpace(plainParameters.PhoneNumber))
                            {
                                responseVal  = 2008;
                                responseText = HomeM8.GetWarningString(2008);
                                error        = true;
                            }
                            else
                            {
                                if (!(new Regex(@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                                                @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-0-9a-z]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$", RegexOptions.IgnoreCase)
                                      .IsMatch(plainParameters.Email)))
                                {
                                    responseVal  = 3;
                                    responseText = HomeM8.GetWarningString(3).Replace("#Parametre#", nameof(plainParameters.Email));
                                    error        = true;
                                }
                                else if (!HomeM8.EmailValid(plainParameters.Email))
                                {
                                    responseVal  = 3008;
                                    responseText = HomeM8.GetWarningString(3008);
                                    error        = true;
                                }
                                else
                                {
                                    if (plainParameters.Password.Length < 6 || plainParameters.Password.Length > 12)
                                    {
                                        responseVal  = 3;
                                        responseText = HomeM8.GetWarningString(3).Replace("#Parametre#", nameof(plainParameters.Password));
                                        error        = true;
                                    }
                                    else
                                    {
                                        if (plainParameters.PhoneNumber.Length != 10 || plainParameters.PhoneNumber[0] != '5')
                                        {
                                            responseVal  = 3;
                                            responseText = HomeM8.GetWarningString(3).Replace("#Parametre#", nameof(plainParameters.PhoneNumber));
                                            error        = true;
                                        }
                                    }
                                }
                            }
                        }

                        #endregion

                        if (!error)
                        {
                            PBKDF2 hashing = new PBKDF2();

                            db.Users.Add(new Users
                            {
                                Username     = username,
                                Userpass     = hashing.Compute(plainParameters.Password),
                                Salt         = hashing.Salt,
                                NameSurname  = plainParameters.NameSurname,
                                ContactInfo  = plainParameters.PhoneNumber,
                                Email        = plainParameters.Email,
                                SharedSecret = sharedSecret,
                                AccessToken  = Guid.NewGuid().ToString("N"),
                                Type         = 2,
                                CreateDate   = DateTime.Now,
                                State        = true
                            });

                            db.FromRegister.Remove(db.FromRegister.FirstOrDefault(each => each.Username == username));

                            await db.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        responseVal  = 2;
                        responseText = HomeM8.GetWarningString(2);
                        error        = true;
                    }
                }
            }

            #endregion

            return(new HttpResponseMessage()
            {
                Content = new StringContent(Security.EncryptAES(sharedSecret, JsonConvert.SerializeObject(new
                {
                    responseVal,
                    responseText,
                })))
            });
        }