Esempio n. 1
0
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            if (FormsAuthentication.CookiesSupported == true)
            {
                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                {
                    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                    Debug.WriteLine(authCookie.Value);

                    if (authCookie != null)
                    {
                        try {
                            FormsAuthenticationTicket authTicket     = FormsAuthentication.Decrypt(authCookie.Value);
                            JavaScriptSerializer      serializer     = new JavaScriptSerializer();
                            UserPrincipalSerialize    serializeModel = serializer.Deserialize <UserPrincipalSerialize>(authTicket.UserData);
                            UserPrincipal             newUser        = new UserPrincipal(serializeModel);
                            Debug.WriteLine(authTicket.Name);

                            HttpContext.Current.User = newUser;
                        } catch (Exception exception) {
                            Console.WriteLine(exception.Message);
                        }
                    }
                }
            }
        }
        public ActionResult Login(Person loginAttempt)
        {
            var person = db.ReadSinglePerson(loginAttempt.email);

            if (person != null && Password.Authenticate(loginAttempt.password, person.password))
            {
                UserPrincipalSerialize user = new UserPrincipalSerialize(person);
                Response.Cookies.Add(AuthTicket.Make(user));

                if (user.IsInRole("Admin"))
                {
                    return(RedirectToAction("Index", "Admin"));
                }
                else if (user.IsInRole("Pharmacist"))
                {
                    return(RedirectToAction("Index", "Pharmacy"));
                }
                else
                {
                    return(RedirectToAction("Index", "User"));
                }
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
            }

            return(View(loginAttempt));
        }
Esempio n. 3
0
        public static HttpCookie Make(UserPrincipalSerialize user)
        {
            // clear any current sign in cookies
            FormsAuthentication.SignOut();
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            user.Store.pharmacists = null;                  // stop any circular references
            string userData = serializer.Serialize(user);

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData);
            string encTicket = FormsAuthentication.Encrypt(authTicket);

            return(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
        }