Ejemplo n.º 1
0
        public async Task <ActionResult> Default(Areas.Administracao.Models.RegisterViewModel model)
        {
            var context     = new ApplicationDbContext();
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            // The default Validators that the UserManager uses are UserValidator and MinimumLengthValidator
            // You can tweak some of the settings as follows
            // This example sets the Password length to be 3 characters
            UserManager.UserValidator = new UserValidator <ApplicationUser>(UserManager)
            {
                AllowOnlyAlphanumericUserNames = false
            };
            UserManager.PasswordValidator = new MinimumLengthValidator(3);


            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                user.HomeTown = model.HomeTown;
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var authManager    = HttpContext.GetOwinContext().Authentication;
                    var claimsIdentity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authManager.SignIn(claimsIdentity);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Customize(Areas.Administracao.Models.RegisterViewModel model)
        {
            var context     = new ApplicationDbContext();
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            // The default Validators that the UserManager uses are UserValidator and MinimumLengthValidator
            // If you want to have complete control over validation then you can write your own validators.
            UserManager.UserValidator     = new MyUserValidation();
            UserManager.PasswordValidator = new MyPasswordValidation();
            UserManager.PasswordHasher    = new PasswordHasher();


            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                user.HomeTown = model.HomeTown;
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var authManager    = HttpContext.GetOwinContext().Authentication;
                    var claimsIdentity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authManager.SignIn(claimsIdentity);
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(View("Index", model));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Register(Areas.Administracao.Models.RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    UserName = model.UserName
                };
                user.HomeTown   = model.HomeTown;
                user.MyUserInfo = new MyUserInfo()
                {
                    FirstName = model.UserName
                };

                // Store Gender as Claim
                user.Claims.Add(new IdentityUserClaim()
                {
                    ClaimType = ClaimTypes.Gender, ClaimValue = "Male"
                });

                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));
        }