private async Task LoginSocial(string name, string email, string userName, string pass) { var vuser = await UserManager.FindByNameAsync(userName); //1. Kiểm tra xem user đã đăng kí vào hệ thống với tài khoản FB chưa if (vuser != null) { //2. Nếu đã đăng kí vào hệ thống thì thực hiện logon //FormsAuthentication.SetAuthCookie(userName, false); await SignInAsync(vuser, false); } //3. Nếu chưa, tạo tài khoản trên hệ thống với username = FB_username, pass = VieGrid!23 #region else { var aspUser = new ApplicationUser() { UserName = userName }; var result = await UserManager.CreateAsync(aspUser, pass); if (result.Succeeded) { var user = new User { Address = "", AspnetId = aspUser.Id, Email = email, Phone = "", UserName = aspUser.UserName, DisplayName = name }; db.User.Add(user); db.SaveChanges(); await SignInAsync(aspUser, isPersistent: false); } } #endregion }
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 }
public async Task<ActionResult> Register(RegisterUserModel model) { if (ModelState.IsValid) { var aspUser = new ApplicationUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(aspUser, model.Password); if (result.Succeeded) { var user = new User { Address = model.Address, AspnetId = aspUser.Id, Email = model.Email, Phone = model.Phone, UserName = aspUser.UserName }; db.User.Add(user); db.SaveChanges(); await SignInAsync(aspUser, isPersistent: false); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }