Exemplo n.º 1
0
        public static void Seed(SaleDbContext context)
        {
            // Tạo các đối tượng quản lý quyền và người dùng
            var userManager = new UserManager<Account>(new UserStore<Account>(context));
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            const string adminRole = "Admin",
                customerRole = "Customer",
                password = "******";

            // Tạo các quyền (vai trò của người dùng trong hệ thống)
            if (!roleManager.RoleExists(adminRole))
                roleManager.Create(new IdentityRole(adminRole));

            if (!roleManager.RoleExists(customerRole))
                roleManager.Create(new IdentityRole(customerRole));

            // Tạo tài khoản Admin
            var adminUser = new Account()
            {
                UserName = "******",
                Profile = new UserProfile()
                {
                    LastName = "Nguyễn Quốc",
                    FirstName = "Anh",
                    BirthDate = new DateTime(1992, 1, 11)
                }
            };
            var result = userManager.Create(adminUser, password);

            // Gán quyền Admin và Teacher cho người dùng vừa tạo
            if (result.Succeeded)
            {
                userManager.AddToRole(adminUser.Id, adminRole);
                userManager.AddToRole(adminUser.Id, customerRole);
            }
        }
Exemplo n.º 2
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "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 Account { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 3
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new Account { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

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

            // If we got this far, something failed, redisplay form
            return View(model);
        }