예제 #1
0
        public ActionResult VerifyUser(string userName, string password)
        {
            var fans = from f in db.Fans
                       select f;

            if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password))
            {

                fans = fans.Where(s => s.UserName.Equals(userName) &&
                                       s.Password.Equals(password));
            }

            if (fans.ToList().Count == 1)
            {
                if (fans.ToList()[0].Permission == 2)
                {
                    status = Status.Admin;
                    this.Session["isAdmin"] = "Yes";
                }
                else
                {
                    status = Status.Logged;
                    this.Session["isAdmin"] = "No";
                }

                this.Session["userID"] = fans.ToList()[0].ID;
                return RedirectToAction("Index", "Blog");
            }
            else
            {
                message = "Incorrect details";
                return RedirectToAction("Login", "FanClub");
            }
        }
예제 #2
0
        public ActionResult LogInFaceBook()
        {
            string app_id = "838250792939146";
            string app_secret = "5e6a472a233200ee63876f238adce751";
            string scope = "publish_actions,manage_pages,user_birthday";

            if (Request["code"] == null)
            {
                Response.Redirect(string.Format(
                    "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                Dictionary<string, string> tokens = new Dictionary<string, string>();
                string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                    app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    string vals = reader.ReadToEnd();
                    foreach(string token in vals.Split('&'))
                    {
                        tokens.Add(token.Substring(0, token.IndexOf("=")),
                                   token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                    }
                }

                access_token = tokens["access_token"];
            }

            if (access_token != "")
            {
                status = Status.Admin;
                this.Session["isAdmin"] = "Yes";
                dynamic me;
                Fan NewFan;

                var fans = from f in db.Fans
                           select f;

                var client = new FacebookClient(FanClubController.access_token);
                try
                {
                    me = client.Get("me", new { fields = "name,id,first_name,last_name,gender" });
                }
                catch (Exception e)
                {
                    return RedirectToAction("Login", "FanClub");
                }

                string username = me.first_name;
                string password = me.last_name;

                fans = fans.Where(s => s.UserName.Equals(username) &&
                                       s.Password.Equals(password));
                if (fans.ToList().Count != 0)
                    this.Session["userID"] = fans.ToList()[0].ID;

                if (this.Session["userID"] == null)
                {
                    NewFan = new Fan(2,
                                     me.first_name,
                                     me.last_name,
                                     me.gender,
                                     DateTime.Today,
                                     1,
                                     me.first_name,
                                     me.last_name
                                     );

                    db.Fans.Add(NewFan);
                    db.SaveChanges();

                    this.Session["userID"] = NewFan.ID;
                }

                return RedirectToAction("Index", "Blog");
            }
            else
            {
                message = "Incorrect details";
                return RedirectToAction("Login", "FanClub");
            }
        }