Beispiel #1
0
        private async void OnGenerateTokenButtonClicked(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.CPFTextBox.Text))
            {
                await new MessageDialog("Você precisa digitar seu CPF!", "CPF inválido").ShowAsync();
                return;
            }

            if (string.IsNullOrEmpty(this.PassTextBox.Password))
            {
                await new MessageDialog("Você precisa digitar sua senha!", "Senha inválida").ShowAsync();
                return;
            }

            string token;
            string cpf     = this.CPFTextBox.Text;
            string rawPass = this.PassTextBox.Password;

            token = await Task.Run(() =>
            {
                string pass = HashServices.HashPassword(rawPass, cpf);

                return(TokenGeneratorService.GenerateToken(pass));
            });

            this.TokenTextBlock.Text           = token;
            this.TokenExpirationTextBlock.Text = "Válido por " + TokenExpirationTimeSeconds + " segundos";
            this.cleanTimer.Start();
        }
Beispiel #2
0
        // This method is private because only authenticated users can see this view.
        public ActionResult Index()
        {
            if (BusinessManager.Instance.GlobalManagers.FindAll().Count == 0)
            {
                ApplicationUserManager userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                ApplicationUser        user;

                if ((user = userManager.FindByEmail("*****@*****.**")) == null)
                {
                    user = new ApplicationUser
                    {
                        Email          = "*****@*****.**",
                        FirstName      = "Ricardo",
                        LastName       = "Souza",
                        EmailConfirmed = true,
                        UserName       = "******",
                        PhoneNumber    = "(19) 99856-0989",
                        UserType       = UserType.GlobalAdmin
                    };

                    userManager.Create(user, "Ricardo12__");
                }

                string  cpf     = "000.000.000-00";
                Address address = new PostalCodeService().GetAdrressFromPostalCode("13024-420").Address;

                address.State = Common.StateTranslator.GetStateName(address.State);

                GlobalManager globalManager = new GlobalManager
                {
                    Address      = address,
                    FirstName    = user.FirstName,
                    LastName     = user.LastName,
                    CPF          = cpf,
                    Email        = user.Email,
                    Password     = user.PasswordHash,
                    Telephone    = user.PhoneNumber,
                    TokenHash    = HashServices.HashPassword("547458", cpf),
                    StreetNumber = 123
                };

                BusinessManager.Instance.GlobalManagers.Add(globalManager);
            }

            if (!Request.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            return(View());
        }
Beispiel #3
0
        public async Task <ActionResult> SaveChanges(EditGlobalAdminViewModel model)
        {
            var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();

            if (ModelState.IsValid)
            {
                ApplicationUser user = await userManager.FindByEmailAsync(model.Email);

                bool   error     = false;
                string tokenHash = null;

                if (!string.IsNullOrEmpty(model.NewPassword))
                {
                    var result = userManager.ChangePassword(user.Id, model.Password, model.NewPassword);

                    if (!result.Succeeded)
                    {
                        ModelState["Password"].Errors.Add("Senha incorreta");
                        error = true;
                    }
                }

                if (!string.IsNullOrEmpty(model.Token))
                {
                    if (string.IsNullOrEmpty(model.Password))
                    {
                        ModelState["Token"].Errors.Add("O token só pode ser alterado se a senha for digitada");
                        error = true;
                    }
                    else
                    {
                        if (!userManager.CheckPassword(user, model.Password))
                        {
                            ModelState["Password"].Errors.Add("Senha incorreta");
                            error = true;
                        }
                        else
                        {
                            tokenHash = HashServices.HashPassword(model.Token, model.CPF);
                        }
                    }
                }

                if (!error)
                {
                    user = await userManager.FindByEmailAsync(model.Email);

                    if (tokenHash == null)
                    {
                        tokenHash = BusinessManager.Instance.GlobalManagers.FindAll().SingleOrDefault(g => g.Email == user.Email).TokenHash;
                    }

                    GlobalManager globalAdmin = model.ToGlobalManager(user.PasswordHash, tokenHash);

                    BusinessManager.Instance.GlobalManagers.Update(globalAdmin);

                    return(RedirectToAction("Index", "GlobalAdmin"));
                }
            }

            return(View("Edit", model));
        }