protected override void Seed(Migx.Web.Models.MigxContext context)
        {
            string plainPass = "******";
            var    harPass   = Crypto.HashPassword(plainPass);

            context.Usuarios.AddOrUpdate(
                new Models.UsuarioModel()
            {
                ID            = 1,
                Nome          = "Administrador",
                DtNascimento  = DateTime.Now.Date,
                Endereco      = "Endereço de teste, 1111",
                Complemento   = "Complemento de teste",
                Cidade        = "São Paulo",
                Estado        = "SP",
                Email         = "*****@*****.**",
                Senha         = harPass,
                Telefone      = "11 12345-6789",
                ConfirmaSenha = harPass,          //Necessario pra não dar erro de validacao
                ConfirmaEmail = "*****@*****.**" //Necessario pra não dar erro de validacao
            }
                );

            if (!context.Users.Any(u => u.Email == "*****@*****.**"))
            {
                var userStore    = new UserStore <AppUserIdentity>(context);
                var userManager  = new AppUserManager(userStore);
                var userToInsert = new AppUserIdentity {
                    UserName = "******", Email = "*****@*****.**"
                };
                userManager.Create(userToInsert, "123456");
            }
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUserIdentity()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #3
0
        public static void Initialize(AppDbContext _db)
        {
            //var building = new Building()
            //{
            //    Name = "отсутствует",
            //    //IsHidden = true
            //};

            //var department = new Department()
            //{
            //    Name = "отсутствует",
            //    //IsHidden = true
            //};

            var appUserIdentity = new AppUserIdentity()
            {
                UserName           = "******",
                NormalizedUserName = "******",
                DisplayName        = "Кантышев А.В.",
                AssosiatedUser     = new AppUser()
                {
                    DisplayName = "Кантышев А.В.",
                    //Building = building,
                    //Department = department,
                    IsActive = true,
                    IsDuty   = false
                }
            };

            _db.Users.Add(appUserIdentity);
            _db.SaveChanges();
        }
        private async Task SignInAsync(AppUserIdentity user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = isPersistent
            }, identity);
        }
 public async Task SignInAsync(AppUserIdentity user, bool isPersistent, bool rememberBrowser)
 {
     // Clear any partial cookies from external or two factor partial sign ins
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
     var userIdentity = await user.GenerateUserIdentityAsync(UserManager);
     if (rememberBrowser)
     {
         var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
         AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity);
     }
     else
     {
         AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity);
     }
 }
        private async Task <SignInStatus> SignInOrTwoFactor(AppUserIdentity user, bool isPersistent)
        {
            if (await UserManager.GetTwoFactorEnabledAsync(user.Id) &&
                !await AuthenticationManager.TwoFactorBrowserRememberedAsync(user.Id))
            {
                var identity = new ClaimsIdentity(DefaultAuthenticationTypes.TwoFactorCookie);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                AuthenticationManager.SignIn(identity);
                return(SignInStatus.RequiresTwoFactorAuthentication);
            }
            await SignInAsync(user, isPersistent, false);

            return(SignInStatus.Success);
        }
        public async Task <ActionResult> Cadastrar(UsuarioModel user)
        {
            if (!ModelState.IsValid)
            {
                ModelState.Clear();
                return(View());
            }

            string plainPass = user.Senha;


            var userIdentity = new AppUserIdentity()
            {
                UserName = user.Email, Email = user.Email
            };
            var result = await UserManager.CreateAsync(userIdentity, plainPass);

            if (result.Succeeded)
            {
                await SignInAsync(userIdentity, isPersistent : false);

                //return RedirectToAction("Index", "Home");
            }

            using (ctx = new MigxContext())
            {
                var hashPass = Crypto.HashPassword(plainPass);
                user.Senha = hashPass;

                user = ctx.Usuarios.Add(user);

                ctx.SaveChanges();
            }
            Session["oUser"] = user;

            Directory.CreateDirectory(Path.Combine(Server.MapPath("~/Content/Images/"), user.ID.ToString())); //Cria o diretório caso ele não exista.
            if (Request.Files != null && Request.Files.Count > 0)
            {
                var file     = Request.Files[0];
                var novoNome = Path.Combine(Server.MapPath("~/Content/Images/"), user.ID.ToString(), "profilePicture" + ".jpg");
                file.SaveAs(novoNome);
            }

            return(RedirectToAction("Index", "TimeLine"));
        }
        public async Task SignInAsync(AppUserIdentity user, bool isPersistent, bool rememberBrowser)
        {
            // Clear any partial cookies from external or two factor partial sign ins
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
            var userIdentity = await user.GenerateUserIdentityAsync(UserManager);

            if (rememberBrowser)
            {
                var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
                AuthenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity, rememberBrowserIdentity);
            }
            else
            {
                AuthenticationManager.SignIn(new AuthenticationProperties {
                    IsPersistent = isPersistent
                }, userIdentity);
            }
        }
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new AppUserIdentity()
                {
                    UserName = model.UserName
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
 private async Task SignInAsync(AppUserIdentity user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new AppUserIdentity() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUserIdentity() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        private async Task<SignInStatus> SignInOrTwoFactor(AppUserIdentity user, bool isPersistent)
        {
            if (await UserManager.GetTwoFactorEnabledAsync(user.Id) &&
                !await AuthenticationManager.TwoFactorBrowserRememberedAsync(user.Id))
            {
                var identity = new ClaimsIdentity(DefaultAuthenticationTypes.TwoFactorCookie);
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
                identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                AuthenticationManager.SignIn(identity);
                return SignInStatus.RequiresTwoFactorAuthentication;
            }
            await SignInAsync(user, isPersistent, false);
            return SignInStatus.Success;

        }