コード例 #1
0
        public async Task <IActionResult> Register(RegisViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser()
                {
                    Email    = model.Email,
                    UserName = model.Email,
                    FullName = model.FullName,
                    Gender   = model.Gender,
                    Address  = model.Address
                };
                var result = await userManager.CreateAsync(user, model.Password);

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

                    return(RedirectToAction("index", "home"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Daftar(RegisViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new IdentityUser
                {
                    UserName = model.Email,
                    Email    = model.Email
                };

                var userCreated = await userManager.CreateAsync(user, model.Password);

                if (userCreated.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "Admin");

                    await signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("Beranda", "Admin"));
                }

                foreach (var err in userCreated.Errors)
                {
                    // Menambahkan error dengan method AddModelError()
                    ModelState.AddModelError("", err.Description);
                }
            }
            return(View(model));
        }