public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new MyIdentityUser {
                    UserName = model.UserName, Login = model.Login
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    IdentityRole defaultRole = RoleUtils.CreateOrGetRole(model.RoleId);
                    RoleUtils.AssignRoleToUser(defaultRole, user);

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

                    // Pour plus d'informations sur l'activation de la confirmation de compte et de la réinitialisation de mot de passe, visitez https://go.microsoft.com/fwlink/?LinkID=320771
                    // Envoyer un message électronique avec ce lien
                    // 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, "Confirmez votre compte", "Confirmez votre compte en cliquant <a href=\"" + callbackUrl + "\">ici</a>");

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

            // Si nous sommes arrivés là, un échec s’est produit. Réafficher le formulaire
            return(View(model));
        }
        public ActionResult Create([Bind(Include = "Id,LastName,FirstName,Phone,Email,SellerAccount,Login,Password")] Seller seller)
        {
            if (ModelState.IsValid)
            {
                // Creation du vendeur dans le SecurityContext du projet ASP.NET
                using (var secudb = new SecurityDbContext())
                {
                    IdentityRole userRole = RoleUtils.CreateOrGetRole("User");
                    UserManager <MyIdentityUser> userManager = new MyIdentityUserManager(new UserStore <MyIdentityUser>(secudb));
                    MyIdentityUser sellerUser = new MyIdentityUser()
                    {
                        UserName = seller.Login, Email = seller.Email, Login = seller.Login
                    };;
                    var result = userManager.Create(sellerUser, seller.Password);
                    if (!result.Succeeded)
                    {
                        throw new System.Exception("database insert fail");
                    }
                    RoleUtils.AssignRoleToUser(userRole, sellerUser);
                }

                // Creation du vendeur dans le ProductContext du projet librairie
                db.Sellers.Add(seller);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(seller));
        }
示例#3
0
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            if (this.Database.CreateIfNotExists())
            {
                IdentityRole userRole  = RoleUtils.CreateOrGetRole("User");
                IdentityRole adminRole = RoleUtils.CreateOrGetRole("Admin");

                UserManager <ApplicationUser> userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(this));
                ApplicationUser admin = new ApplicationUser()
                {
                    UserName = "******", Email = "*****@*****.**", Login = "******"
                };
                var result = userManager.Create(admin, "Lolo!421");
                if (!result.Succeeded)
                {
                    this.Database.Delete();
                    throw new System.Exception("database insert fail");
                }
                RoleUtils.AssignRoleToUser(adminRole, admin);
            }
        }
        public SecurityDbContext()
            : base("DefaultSecurityConnection", throwIfV1Schema: false)
        {
            if (this.Database.CreateIfNotExists())
            {
                IdentityRole userRole  = RoleUtils.CreateOrGetRole("User");
                IdentityRole adminRole = RoleUtils.CreateOrGetRole("Admin");

                UserManager <MyIdentityUser> userManager = new MyIdentityUserManager(new UserStore <MyIdentityUser>(this));
                MyIdentityUser admin = new MyIdentityUser()
                {
                    UserName = "******", Email = "*****@*****.**", Login = "******"
                };
                var result = userManager.Create(admin, "Admin!123");
                if (!result.Succeeded)
                {
                    this.Database.Delete();
                    throw new System.Exception("database insert fail");
                }
                RoleUtils.AssignRoleToUser(adminRole, admin);
            }
        }
        public SecurityDbContext()
            : base("DefaultSecurityConnection", throwIfV1Schema: false)
        {
            if (this.Database.CreateIfNotExists())
            {
                // Créer un role Administrateur et un role User
                IdentityRole adminRole = RoleUtils.CreateOrGetRole("Admin");
                IdentityRole userRole  = RoleUtils.CreateOrGetRole("User");

                // Créer un utilisateur dont le login sera "admin" avec le mot de passe "admin" et le role Administrateur
                UserManager <MyIdentityUser> userManager = new MyIdentityUserManager(new UserStore <MyIdentityUser>(this));
                MyIdentityUser admin = new MyIdentityUser()
                {
                    UserName = "******", Email = "*****@*****.**", Login = "******"
                };
                var result = userManager.Create(admin, "Admin!123");
                if (!result.Succeeded)
                {
                    this.Database.Delete();
                    throw new System.Exception("database insert fail");
                }
                RoleUtils.AssignRoleToUser(adminRole, admin);
            }
        }