public ActionResult LoginAdmin(ADMIN usermodel) { //New dbConnect using (MiniShopEntities db = new MiniShopEntities()) { //Lấy username và password ở bản ghi đầu tiên var user = db.ADMINs.Where(x => x.TenDNAD == usermodel.TenDNAD && x.MatKhauAD == usermodel.MatKhauAD).FirstOrDefault(); if (user == null) { ViewBag.error = "Email or Password is fail"; return(View("LoginAdmin", usermodel)); } else { //ViewBag.avatar = user.Avatar; //ViewBag.Online = user.IsActive; //Session["Online"] =user.IsActive; //Session["Avatar"] = user.Avatar; Session["Email"] = user.EmailAD; Session["Username"] = user.TenDNAD; //return View(user) return(RedirectToAction("Index", "AdminCRUD")); } } }
public ActionResult GoogleLoginCallback() { var claimsPrincipal = HttpContext.User.Identity as ClaimsIdentity; var loginInfo = SSO.GetLoginInfo(claimsPrincipal); if (loginInfo == null) { return(RedirectToAction("DangNhap")); } MiniShopEntities db = new MiniShopEntities(); //DbContext var user = db.KhachHangs.FirstOrDefault(x => x.Email == loginInfo.emailaddress); if (user == null) { user = new KhachHang { Email = loginInfo.emailaddress, HoTenKH = loginInfo.name, DiaChiKH = loginInfo.nameidentifier, }; db.KhachHangs.Add(user); db.SaveChanges(); } Session["username"] = loginInfo.name; Session["makh"] = user.MaKH; var ident = new ClaimsIdentity( new[] { // adding following 2 claim just for supporting default antiforgery provider new Claim(ClaimTypes.NameIdentifier, user.Email), new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), new Claim(ClaimTypes.Name, user.HoTenKH), new Claim(ClaimTypes.Email, user.Email), // optionally you could add roles if any new Claim(ClaimTypes.Role, "User") }, CookieAuthenticationDefaults.AuthenticationType); HttpContext.GetOwinContext().Authentication.SignIn( new AuthenticationProperties { IsPersistent = false }, ident); return(Redirect("~/")); }