Пример #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;
                        string roles = string.Empty;

                        using (TicketBookingEntities db = new TicketBookingEntities())
                        {
                            RegisteredUser user = db.RegisteredUsers.SingleOrDefault(x => x.Username == username);

                            roles = user.Membership.Role;
                        }
                        //let us extract the roles from our own custom cookie

                        //Let us set the Pricipal with our user specific details
                        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(
                          new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
                    }
                    catch (Exception)
                    {
                        //somehting went wrong
                    }
                }
            }
        }
Пример #2
0
        public ActionResult FacebookCallback(string code)
        {
            var fb = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id = System.Configuration.ConfigurationManager.AppSettings["FacebookAppID"],
                client_secret = System.Configuration.ConfigurationManager.AppSettings["FacebookAppSecret"],
                redirect_uri = RedirectUri.AbsoluteUri,
                code = code
            });

            var accessToken = result.access_token;
            fb.AccessToken = accessToken;
            dynamic me = fb.Get("me?fields=email");

            if (!IsUserRegistered(me.mail))
            {

                using (TicketBookingEntities db = new TicketBookingEntities())
                {
                    string password = DateTime.Now.ToString();
                    RegisteredUser user = new Models.RegisteredUser() { Username = me.email, Password = password, Email = me.email };
                    db.RegisteredUsers.Add(user);
                    db.Memberships.Add(new Models.Membership() { UserID = user.ID, Role = db.Roles.Single(x => x.ID == 1).Name });
                    db.SaveChanges();
                }
            }

            FormsAuthentication.SetAuthCookie(me.email, false);
            Session["login"] = true;
            Session["username"] = me.email;
            return RedirectToAction("Index", "Home");
        }
Пример #3
0
 public ActionResult Create()
 {
     string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
     TicketBookingEntities db = new TicketBookingEntities();
     int userid = db.RegisteredUsers.Single(x => x.Username == username).ID;
     CardInfo model = new CardInfo() { UserID = userid };
     ViewBag.Providers = db.CardProviders;
     return View(model);
 }
Пример #4
0
        public ActionResult Index()
        {
            string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;

            TicketBookingEntities db = new TicketBookingEntities();
            int userid = db.RegisteredUsers.Single(x => x.Username == username).ID;
            var model = db.CardInfoes.Where(x => x.UserID == userid);

            return View(model);
        }
Пример #5
0
        public ActionResult Create(CardInfo card)
        {
            if (ModelState.IsValid)
            {
                string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                TicketBookingEntities db = new TicketBookingEntities();
                int userid = db.RegisteredUsers.Single(x => x.Username == username).ID;
                card.UserID = userid;
                db.CardInfoes.Add(card);
                db.SaveChanges();
                return RedirectToAction("Details", "Account");
            }

            ModelState.AddModelError("error", "Check your data input!!!");
            return View(card);
        }
Пример #6
0
 private bool IsUserValid(string username, string password)
 {
     TicketBookingEntities db = new TicketBookingEntities();
     return db.RegisteredUsers.Any(x => x.Username == username && x.Password == password);
 }
Пример #7
0
 private bool IsUserRegistered(string username)
 {
     using (TicketBookingEntities db = new TicketBookingEntities())
     {
         return db.RegisteredUsers.Any(x => x.Username == username);
     }
 }
Пример #8
0
        public ActionResult Register(RegisteredUser user)
        {
            if (ModelState.IsValid)
            {
                using (TicketBookingEntities db = new TicketBookingEntities())
                {
                    if (!db.RegisteredUsers.Any(x => x.Username == user.Username))
                    {
                        db.RegisteredUsers.Add(user);
                        db.Memberships.Add(new Models.Membership() { UserID = user.ID, Role = db.Roles.Single(x => x.ID == 1).Name });
                        db.SaveChanges();

                        return RedirectToAction("Login", "Account");
                    }
                }
            }

            ModelState.AddModelError("error", "Something's wrong!!!");
            return View(user);
        }