コード例 #1
0
        public ActionResult GoogleLoginCallback()
        {
            var claimsPrincipal = HttpContext.User.Identity as ClaimsIdentity;

            var loginInfo = SSO.GetLoginInfo(claimsPrincipal);

            if (loginInfo == null)
            {
                return(RedirectToAction("Index"));
            }


            ASM_BookEntities db = new ASM_BookEntities(); //DbContext
            var user            = db.Users.FirstOrDefault(x => x.Email == loginInfo.emailaddress);

            if (user == null)
            {
                user = new User
                {
                    Email    = loginInfo.emailaddress,
                    Password = loginInfo.nameidentifier,
                    UserName = loginInfo.givenname,
                };
                db.Users.Add(user);
                db.SaveChanges();
            }

            Session["makh"] = user.MaKH;

            Session["usename"] = loginInfo.givenname;
            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.UserName),
                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("~/"));
        }
コード例 #2
0
        public ActionResult LoginAdmin(User usermodel)
        {
            //New dbConnect
            using (ASM_BookEntities db = new ASM_BookEntities())
            {
                //Lấy username và password ở bản ghi đầu tiên
                var user = db.Admins.Where(x => x.UserName == usermodel.UserName && x.Password == usermodel.Password).FirstOrDefault();
                if (user == null)
                {
                    ViewBag.error = "Email or Password is fail";
                    return(View("LoginAdmin", usermodel));
                }
                else
                {
                    Session["Email"]    = user.Email;
                    Session["Username"] = user.UserName;
                    //return View(user)

                    return(RedirectToAction("Index", "AdminCRUD"));
                }
            }
        }