Exemple #1
0
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);

            #region Add Admin and Role for Admin
            using (var context = new ApplicationDbContext())
            {
                var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
                var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

                var aspUser = UserManager.FindByName(Constant.USER_USERNAME);
                if (aspUser != null)
                {
                    UserManager.RemovePassword(aspUser.Id);
                    UserManager.AddPassword(aspUser.Id, Constant.USER_PASS);
                }
                else
                {
                    aspUser = new ApplicationUser() { UserName = Constant.USER_USERNAME };
                    var result = UserManager.Create(aspUser, Constant.USER_PASS);
                    if (result.Succeeded)
                    {
                        var user = new User
                        {
                            UserName = Constant.USER_USERNAME,
                            Email = Constant.USER_EMAIL,
                            Phone = Constant.USER_PHONE,
                            Address = Constant.USER_ADDRESS,
                            AspnetId = aspUser.Id
                        };
                        using (var db = new TShirtEntities())
                        {
                            db.User.Add(user);
                            db.SaveChanges();
                        }
                    }
                }
                // Create Role
                IdentityResult roleResult = null;
                if (!RoleManager.RoleExists(Constant.ROLES_ADMIN))
                {
                    roleResult = RoleManager.Create(new IdentityRole(Constant.ROLES_ADMIN));
                }

                // Add role to admin
                if (roleResult != null && roleResult.Succeeded) UserManager.AddToRole(aspUser.Id, Constant.ROLES_ADMIN);

            }
            #endregion

            #region Add Constants Config
            var lstConfig = new List<Config>();
            using (var db = new TShirtEntities())
            {
                var fb = db.Config.FirstOrDefaultAsync(m => m.Code == Constant.CODE_MESS_FACEBOOK);
                var price = db.Config.FirstOrDefaultAsync(m => m.Code == Constant.CODE_PRICE_DESIGN);
                if (fb == null)
                {
                    lstConfig.Add(new Config() { Code = Constant.CODE_MESS_FACEBOOK, Value = "Website thiết kế áo chuyên nghiệp", Description = "Nội dung khi chia sẻ trên facebook" });
                }
                if (price == null)
                {
                    lstConfig.Add(new Config() { Code = Constant.CODE_PRICE_DESIGN, Value = "15000", Description = "Giá một icon design" });
                }
                if (lstConfig.Count > 0)
                {
                    db.Config.AddRange(lstConfig);
                    db.SaveChanges();
                }
            }
            #endregion
        }