Ejemplo n.º 1
0
        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));
        }
        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);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }