Example #1
0
 protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
 {
     if (FormsAuthentication.CookiesSupported == true)
     {
         if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
         {
             try
             {
                 //let us take out the username now
                 string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                 using (var db = new McKarenDb())
                 {
                     var user = db.Users.FirstOrDefault(x => x.UserName.ToLower().Equals(username.ToLower()));
                     if (user != null)
                     {
                         CustomPrincipal newUser = new CustomPrincipal(user.UserName);
                         newUser.roles = user.Roles.Select(x => x.Id).ToList();
                         System.Web.HttpContext.Current.User = newUser;
                     }
                 }
             }
             catch (Exception ex)
             {
                 //went wrong
             }
         }
     }
 }
Example #2
0
        public ActionResult Auth()
        {
            var ggId           = GoogleAuthenticateHelper.GetDefaultGoogleId();
            var authenProvider = new GoogleAuthenticateHelper(ggId.ClientId, ggId.ClientSecret);
            var re             = authenProvider.Resolve(new GoogleAuthenticateHelper.GoogleTokenCallbackQuery {
                Code    = Request.QueryString["code"],
                FullUrl = Request.RawUrl
            }, Url.Content("~/Auth", true));

            if (re == null)
            {
                return(RedirectToAction("Login", "Account"));
            }
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                1,
                re.Email,
                DateTime.Now,
                DateTime.Now.AddDays(7),
                false,
                ""
                );

            string     encTicket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);

            Response.Cookies.Add(faCookie);
            using (var db = new McKarenDb())
            {
                db.Users.Add(new User
                {
                    Id           = Guid.NewGuid.ToString().ToUpper(),
                    UserName     = re.Email,
                    Active       = false,
                    AuthProvider = "Google",
                    Email        = re.Email,
                    Password     = string.Empty
                });
                db.SaveChanges();
            }

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