public ActionResult FacebookCallback(string code) { var fb = new FacebookClient(); dynamic result = fb.Post("oauth/access_token", new { client_id = ConfigurationManager.AppSettings["FbAppId"], client_secret = ConfigurationManager.AppSettings["FbAppSecret"], redirect_uri = RedirectUriFacebook.AbsoluteUri, code = code }); var accessToken = result.access_token; if (!string.IsNullOrEmpty(accessToken)) { fb.AccessToken = accessToken; dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email,birthday"); string email = string.IsNullOrEmpty(me.email) ? "" : me.email; DateTime birthday = string.IsNullOrEmpty(me.birthday) ? null : Convert.ToDateTime(me.birthday); string firstName = string.IsNullOrEmpty(me.first_name) ? "" : me.first_name; string middelName = string.IsNullOrEmpty(me.middle_name) ? "" : me.middle_name; string lastName = string.IsNullOrEmpty(me.last_name) ? "" : me.last_name; if (string.IsNullOrEmpty(email)) { return(HttpNotFound()); } var user = new User { Email = email, UserName = email, Name = firstName + " " + middelName + " " + lastName, CreateDate = DateTime.Now, Birthday = birthday, Password = Guid.NewGuid().ToString() }; var resultInsert = new TAMS.DAL.UserContext().InsertForFacebook(user); if (resultInsert > 0) { var Useression = new UserLogin(); Useression.UserName = user.UserName; Useression.UserId = user.Id; Session.Add(CommonConstants.USER_SESSION, Useression); } } return(Redirect("/")); }
public ActionResult SignUp(RegisterModel model) { if (ModelState.IsValid) { var db = new TAMS.DAL.UserContext(); if (db.IsExistUserName(model.UserName)) { ModelState.AddModelError("", "Tên Đăng nhập đã tồn tại."); } else if (db.IsEmail(model.Email)) { ModelState.AddModelError("", "Email đã được sử dụng."); } else if (model.Password != model.ConfirmPassword) { ModelState.AddModelError("", "Mật khẩu không khớp"); } else { var user = new User(); user.UserName = model.UserName; user.Name = model.Name; user.Email = model.Email; user.Password = model.Password; user.Birthday = model.Birthday; var result = db.Insert(user); if (result > 0) { ViewBag.Success = "Đăng kí thành công"; model = new RegisterModel(); return(RedirectToAction("Index", "HomeAdmin")); } else { ModelState.AddModelError("", "Đăng kí thất bại"); } } } return(View(model)); }