Пример #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            DataService dataService = new DataService();

            if (ModelState.IsValid)
            {
                string pass      = sha256_hash(model.Password);
                Admin  novoAdmin = new Admin
                {
                    name     = model.Nome,
                    username = model.Username,
                    email    = model.Email,
                    password = pass
                };
                ApplicationDbContext context = new ApplicationDbContext();
                var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                if (!roleManager.RoleExists("Admin"))
                {
                    var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
                    role.Name = "Admin";
                    roleManager.Create(role);
                }
                var user = new ApplicationUser {
                    UserName = model.Username, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await this.UserManager.AddToRoleAsync(user.Id, "Admin");
                }
                Admin aaaa = new Admin();
                aaaa = await dataService.GetAdminByIdAsync(model.Username);

                bool a = false;
                if (aaaa == null && novoAdmin.username != "toze")
                {
                    a = await dataService.AddAdminAsync(novoAdmin);
                }

                //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                // Para obter mais informações sobre como habilitar a confirmação da conta e redefinição de senha, visite https://go.microsoft.com/fwlink/?LinkID=320771
                // Enviar um email com este link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirmar sua conta", "Confirme sua conta clicando <a href=\"" + callbackUrl + "\">aqui</a>");

                if (a == true)
                {
                    Success(string.Format("'<b>{0}</b>' created with success!", novoAdmin.username), true);
                    return(RedirectToAction("ListaAdmin", "Home"));
                }
                ModelState.AddModelError("", "Admin already registered.");
            }

            // Se chegamos até aqui e houver alguma falha, exiba novamente o formulário
            return(View(model));
        }