public async Task <ActionResult> Register(RegisterViewModel model, bool isAjax)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var user = new MyUser
             {
                 UserName   = model.Email,
                 Email      = model.Email,
                 FullName   = model.FullName,
                 GenderId   = model.Gender,
                 GenderName = model.Gender == (int)EnumCore.Classification.gioi_tinh_nam ? "Nam" : (model.Gender == (int)EnumCore.Classification.gioi_tinh_nu ? "Nữ" : "Khác")
             };
             var result = UserManager.Create(user, model.Password);
             if (result.Succeeded)
             {
                 //  UserManager.SetLockoutEnabled(user.Id, true);
                 //UserManager.Update(user);
                 //string code = UserManager.GenerateEmailConfirmationToken(user.Id);
                 //var callbackUrl = Url.Action("ConfirmEmail", "AccountAdmin", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                 //EmailService email = new EmailService();
                 //IdentityMessage message = new IdentityMessage();
                 //message.Body = string.Format("Để xác thực email vui lòng nhấp vào liên kết: <a href='{0}'>Tại đây</a>", callbackUrl);
                 //message.Subject = "Xác thực tài khoản";
                 //message.Destination = user.Email;
                 //try
                 //{
                 //    await email.SendAsync(message, EmailService.EmailAdmin, EmailService.EmailAdmin,
                 //    EmailService.EmailAdminPassword, EmailService.EmailAdminSMTP, EmailService.Portmail, true);
                 //}
                 //catch { }
                 if (isAjax)
                 {
                     return(Json(true, JsonRequestBehavior.AllowGet));
                 }
                 else
                 {
                     return(RedirectToAction("Login", "Account"));
                 }
             }
             AddErrors(result);
         }
         if (isAjax)
         {
             return(Json(false, JsonRequestBehavior.AllowGet));
         }
         else
         {
             return(View(model));
         }
     }
     catch (Exception e)
     {
         cms_db.AddToExceptionLog("function Register Public", "Account", e.ToString());
         return(null);
     }
 }
Exemple #2
0
        protected override void Seed(MyDbContext context)
        {
            var userManager = new MyUserManager(new UserStore <User>(context));

            var roles = new List <IdentityRole>
            {
                new IdentityRole("Admin"),
                new IdentityRole("User"),
                new IdentityRole("Manager")
            };

            foreach (var role in roles)
            {
                context.Roles.Add(role);
            }

            context.SaveChanges();

            // Create users.
            var users = new[]
            {
                new User
                {
                    Email    = "*****@*****.**",
                    UserName = "******"
                },
                new User
                {
                    Email       = "*****@*****.**",
                    UserName    = "******",
                    PhoneNumber = "(095) 333-33-33",
                    FirstName   = "Valera",
                    LastName    = "Revolver",
                    Purchased   = 3,
                    Flowers     = new List <Flower> {
                    },
                }
            };

            foreach (var user in users)
            {
                userManager.Create(user);
                userManager.AddPassword(user.Id, "Password_1");
            }
            context.SaveChanges();

            // Set roles to main users.
            userManager.AddToRole(users[0].Id, "Admin");
            userManager.AddToRole(users[1].Id, "Manager");
            context.SaveChanges();

            // Set roles to other users.
            for (int i = 2; i < users.Length; i++)
            {
                userManager.AddToRole(users[i].Id, "User");
            }
            context.SaveChanges();

            base.Seed(context);
        }
Exemple #3
0
        protected override void Seed(ExtJS_Store.DAL.ApplicationContext context)
        {
            var userManager = new MyUserManager(new UserStore <User>(context));
            var roleManager = new MyRoleManager(new RoleStore <MyRole>(context));

            // создаем две роли
            var role1 = new MyRole {
                Name = "admin"
            };
            var role2 = new MyRole {
                Name = "user"
            };

            // добавляем роли в бд
            roleManager.Create(role1);
            roleManager.Create(role2);

            // создаем пользователей
            var user = new User {
                Email = "*****@*****.**", UserName = "******"
            };
            string password = "******";
            var    result   = userManager.Create(user, password);

            // если создание пользователя прошло успешно
            if (result.Succeeded)
            {
                // добавляем для пользователя роли
                userManager.AddToRole(user.Id, role1.Name);
                userManager.AddToRole(user.Id, role2.Name);
            }

            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
Exemple #4
0
        private void AddUsers(ModelsContext context)
        {
            var users = new[]
            {
                new { Name = "admin", Role = "admin", Email = "*****@*****.**" },
                new { Name = "worker", Role = "worker", Email = "*****@*****.**" },
                new { Name = "user", Role = "user", Email = "*****@*****.**" }
            };
            var uMan = new MyUserManager(new UserStore <MyUser>(context));

            if (uMan.Users.Count() == 0)
            {
                foreach (var user in users)
                {
                    var nUser = new MyUser()
                    {
                        UserName = user.Name, Email = user.Email
                    };
                    var result = uMan.Create(nUser, nUser.UserName);
                    if (result.Succeeded)
                    {
                        var res = uMan.AddToRole(nUser.Id, user.Role);
                        if (!res.Succeeded)
                        {
                            throw new Exception("Nie uda³o siê dodaæ usera do roli");
                        }
                    }
                    else
                    {
                        var errors = "";
                        foreach (string error in result.Errors)
                        {
                            errors += error + "\n";
                        }
                        throw new Exception("Nie uda³o siê dodaæ users\n" + errors);
                    }
                }
            }
        }
Exemple #5
0
        protected override void Seed(Infrastructure.DbContext.MyIdentityDbContext context)
        {
            MyUserManager userManager = new MyUserManager(new UserStore <MyUser>(context));
            MyRoleManager roleManager = new MyRoleManager(new RoleStore <MyRole>(context));

            string roleName = "Administrator";
            string userName = "******";
            string password = "******";
            string email    = "*****@*****.**";

            if (!roleManager.RoleExists(roleName))
            {
                roleManager.Create(new MyRole(roleName));
            }

            MyUser user = userManager.FindByName(userName);

            if (user == null)
            {
                userManager.Create(new MyUser {
                    UserName = userName, Email = email
                },
                                   password);
                user = userManager.FindByName(userName);
            }

            if (!userManager.IsInRole(user.Id, roleName))
            {
                userManager.AddToRole(user.Id, roleName);
            }
            foreach (MyUser dbUser in userManager.Users)
            {
                if (dbUser.Country == Countries.None)
                {
                    dbUser.SetCountryFromCity(dbUser.City);
                }
            }
            context.SaveChanges();
        }
Exemple #6
0
        protected override void Seed(ExtStore2.DAL.ApplicationContext context)
        {
            var userManager = new MyUserManager(new UserStore <User>(context));
            var roleManager = new MyRoleManager(new RoleStore <MyRole>(context));

            // создаем две роли
            var role1 = new MyRole {
                Name = "admin"
            };
            var role2 = new MyRole {
                Name = "user"
            };

            // добавляем роли в бд
            roleManager.Create(role1);
            roleManager.Create(role2);

            // создаем пользователей
            var user = new User
            {
                Email    = "*****@*****.**",
                UserName = "******",
                Name     = "Admin",
                Address  = "My address",
                Code     = "My code",
                Discount = 50
            };
            string password = "******";
            var    result   = userManager.Create(user, password);

            // если создание пользователя прошло успешно
            if (result.Succeeded)
            {
                // добавляем для пользователя роли
                userManager.AddToRole(user.Id, role1.Name);
                userManager.AddToRole(user.Id, role2.Name);
            }

            var products = new List <Product>
            {
                new Product {
                    Name = "Продукт 1", Price = 100, Category = "Category", Code = "Code 1"
                },
                new Product {
                    Name = "Продукт 2", Price = 500, Category = "Category", Code = "Code 2"
                },
                new Product {
                    Name = "Новый продукт 3", Price = 8350, Category = "New category", Code = "New code 1"
                },
                new Product {
                    Name = "Новый продукт 4", Price = 15613, Category = "New category", Code = "New code 2"
                }
            };

            for (int i = 1; i <= 50; i++)
            {
                products.Add(new Product {
                    Name = "Product " + i, Price = i * 1000 + i * 3, Category = "Category " + i, Code = "Code " + i
                });
            }
            ;

            products.ForEach(s => context.Products.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();
        }