Exemple #1
0
        public async void Register()
        {
            RegAnimate = true;
            bool _Error = false;

            //Diagnostics
            string Message    = string.Empty;
            string StackTrace = string.Empty;

            //Register user credentials
            await Task.Run(() =>
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(Username))
                    {
                        throw new ArgumentNullException("Username cannot be empty. Please try again");
                    }
                    if (string.IsNullOrWhiteSpace(Password))
                    {
                        throw new ArgumentNullException("Password cannot be empty. Please try again");
                    }
                    if (string.IsNullOrWhiteSpace(ConfirmPassword))
                    {
                        throw new ArgumentNullException("Password cannot be empty. Please try again");
                    }
                    if (!Password.Equals(ConfirmPassword))
                    {
                        throw new ArgumentNullException("Passwords do not match");
                    }

                    //Regex validation = new Regex(_EmailValidator, RegexOptions.CultureInvariant);
                    //if (!validation.IsMatch(Username))
                    //    throw new InvalidDataException("Your username does not appear to be an email address. Please try again");

                    //First & Last Name
                    if (string.IsNullOrWhiteSpace(FirstName))
                    {
                        throw new ArgumentNullException("First name cannot be empty");
                    }
                    if (string.IsNullOrWhiteSpace(LastName))
                    {
                        throw new ArgumentNullException("Last name cannot be empty");
                    }

                    //Generate Account
                    Account obj              = new Account();
                    obj.Contact_ID_Ref       = Guid.NewGuid().ToString();
                    obj.FirstName            = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(FirstName);
                    obj.LastName             = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(LastName);
                    obj.SiteUser_DisplayName = $"{ obj.FirstName } { obj.LastName }";

                    obj.Sys_Creation    = DateTime.Now;
                    obj.Sys_Transaction = DateTime.Now;

                    //Has the passwords on account generation and on login
                    PasswordEncoder hasher = new PasswordEncoder();
                    obj.Username           = Username;
                    obj.Password           = Password;

                    obj.Mobile          = Mobile;
                    obj.Work            = Work;
                    obj.Email           = Username;
                    obj.Home            = Home;
                    obj.Avatar          = Avatar_File;
                    obj.Avatar_FilePath = Avatar;

                    if (accountManager != null)
                    {
                        var hashedPassword = hasher.Encode(Password, EncryptType.SHA_512);
                        if (!accountManager.AuthenticateSiteUser_ByCredentials(obj.Username, hashedPassword))
                        {
                            //Add Accounts via the server Via a background service. Update the Guid based on the Id
                            Task.Run(() =>
                            {
                                try
                                {
                                    DataVaultAccountServiceClient serviceClient = new DataVaultAccountServiceClient(ConfigurationManager.InSecurePublicBinding(), new System.ServiceModel.EndpointAddress(Constants.AccountsInSecureUrl));

                                    var response = serviceClient._Generate_AccountForUser(LocalMapper.MapAccount_ToServer(obj));
                                    if (response.Errors.Count != 0)
                                    {
                                        response.Errors.ForEach(w =>
                                        {
                                            var log = LocalMapper.Map_LogWithError(w, string.Empty, Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

                                            if (this.logging != null)
                                            {
                                                this.logging.AddLog(log);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        accountManager.Update_AccountGuidByUsername(Username, response.Contact_ID);
                                        Constants.InMemory_ContactID = response.Contact_ID;
                                    }
                                }
                                catch (Exception oEx)
                                {
                                    string sMessage    = string.Empty;
                                    string sStackTrace = string.Empty;

                                    if (oEx.InnerException != null)
                                    {
                                        sMessage    = oEx.InnerException.Message;
                                        sStackTrace = oEx.InnerException.StackTrace;
                                    }
                                    else
                                    {
                                        sMessage    = oEx.Message;
                                        sStackTrace = oEx.StackTrace;
                                    }

                                    var mEx = new Exceptions(logging, sMessage, sStackTrace);
                                    if (mEx != null)
                                    {
                                        mEx.HandleException(mEx, logging);
                                    }
                                }
                            });

                            obj.Password = hashedPassword;
                            accountManager.AddAccount_ByHashedPassword(obj);
                        }
                        else
                        {
                            throw new MemberAccessException("This account already exists. Please try a different username");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("Dependency cannot be null. Please review the source code and contact site administrator for assistance");
                    }
                }
                catch (Exception ex)
                {
                    HasError = true;
                    _Error   = true;

                    if (ex.Message.Contains("Passwords do not match"))
                    {
                        ErrorMessage = "Passwords do not match";
                    }
                    else
                    {
                        ErrorMessage = "Invalid username or password";
                    }

                    if (ex.InnerException != null)
                    {
                        Message    = ex.InnerException.Message;
                        StackTrace = ex.InnerException.StackTrace;
                    }
                    else
                    {
                        Message    = ex.Message;
                        StackTrace = ex.StackTrace;
                    }

                    var pEx = new Exceptions(logging, Message, StackTrace);
                    if (pEx != null)
                    {
                        pEx.HandleException(pEx, logging);
                    }
                }
            }).WaitUntilComplete(TimeSpan.FromSeconds(4), () =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    RegAnimate = false;

                    if (!_Error)
                    {
                        if (navigation != null)
                        {
                            navigation.GoBackAsync(true);
                        }
                    }
                    else
                    {
                        dialogue.ShowAlert("mmm...Something went wrong", Message);
                    }
                });
            });
        }