コード例 #1
0
        /// <summary>
        /// Get the authorisation token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Dictionary <string, string> GetAccessToken(string code, string scope, string redirectUrl)
        {
            Dictionary <string, string> tokens = new Dictionary <string, string>();
            // string clientId = "125788630880182";
            //string clientSecret = "350d206a2dd0cc775f1b76fdafe859f3";
            // App ID  208878159224995
            // App Secret  e0ac797bf3135232df717ed976c27453


            string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",
                                       FacebookApp.ClientId(), redirectUrl, FacebookApp.ClientSecret(), code, scope);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

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

                foreach (string token in retVal.Split('&'))
                {
                    tokens.Add(token.Substring(0, token.IndexOf("=")),
                               token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                }
            }
            return(tokens);
        }
コード例 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            FacebookLoginHelper helper = new FacebookLoginHelper();

            if (Request.Params.AllKeys.Contains("code"))
            {
                Dictionary <string, string> dicAccessToken = helper.GetAccessToken(Request["code"].ToString(), FacebookApp.Scope(), FacebookApp.RegRedirectUrl());

                var     accessToken = dicAccessToken["access_token"];
                var     client      = new FacebookClient(accessToken);
                dynamic me          = client.Get("me");
                Session["fbUserId"] = me["id"];


                // Check Database for Facebook Lookup
                string Results = "";
                Results = Validate_Facebook(me["id"]);
                if (Results.Length > 4) // User already has an account
                {
                    FormsAuthentication.RedirectFromLoginPage(Results, false);
                }

                else // User Doesn't have an account yet
                {
                    img_fbUserImage.ImageUrl = "https://graph.facebook.com/" + me["id"] + "/picture?type=large";
                    lit_fbUserName.Text      = me["first_name"];


                    if (!IsPostBack)
                    {
                        this.Email.Text    = me["email"];
                        this.UserName.Text = me["username"];
                    }

                    // Write Friend IDs from JSON
                    var     json = new WebClient().DownloadString("https://api.facebook.com/method/friends.getAppUsers?access_token=" + accessToken + "&format=json");
                    var     jss  = new JavaScriptSerializer();
                    dynamic data = jss.Deserialize <dynamic>(json);
                    //foreach (dynamic friend in data)
                    //{
                    //    Response.Write(friend);
                    //    Response.Write("<br>");
                    //}
                }
            }

            else
            {
                Response.Redirect("Error.aspx");
            }
        }
コード例 #3
0
        protected void btn_Register_Click(object sender, EventArgs e)
        {
            if (Page.IsValid == true)
            {
                int Results = 0;
                Results = Validate_UserName(UserName.Text.Trim());
                if (Results == 1)
                {
                    ErrorMessage.Text = "That Username Already Exists, Please Select Another";
                }
                else
                {
                    // Create User
                    string sUserName = UserName.Text.Trim();
                    string sPassword = Password.Text.Trim();
                    string sEmail    = Email.Text.Trim();

                    var    md5      = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    var    encoding = new System.Text.ASCIIEncoding();
                    byte[] pwBytes  = md5.ComputeHash(encoding.GetBytes(sPassword));
                    string passwordHash;
                    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                    passwordHash = enc.GetString(pwBytes);


                    SqlConnection con       = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
                    SqlCommand    cmdinsert = new SqlCommand();
                    cmdinsert.CommandType = CommandType.StoredProcedure;
                    cmdinsert.CommandText = "User_Create";
                    cmdinsert.Parameters.Add("@Username", SqlDbType.VarChar, 100).Value   = sUserName;
                    cmdinsert.Parameters.Add("@Password", SqlDbType.VarChar, 100).Value   = passwordHash;
                    cmdinsert.Parameters.Add("@Email", SqlDbType.VarChar, 255).Value      = sEmail;
                    cmdinsert.Parameters.Add("@FacebookId", SqlDbType.VarChar, 255).Value = Session["fbUserId"].ToString();
                    // Add New Book Commands
                    cmdinsert.Parameters.Add("@collectionName", SqlDbType.VarChar, 100).Value        = sUserName + "'s First Book";
                    cmdinsert.Parameters.Add("@collectionDescription", SqlDbType.VarChar, 255).Value = "This first book was created automatically for " + sUserName + ".";
                    cmdinsert.Parameters.Add("@groupName", SqlDbType.VarChar, 100).Value             = sUserName + "'s First Page";
                    cmdinsert.Parameters.Add("@groupDescription", SqlDbType.VarChar, 255).Value      = "This page was created along with the book.";


                    cmdinsert.Connection = con;

                    try
                    {
                        con.Open();
                        //cmdinsert.ExecuteNonQuery();
                        Int32 newBookId = Convert.ToInt32(cmdinsert.ExecuteScalar());


                        var webClientful = new WebClient();
                        using (var fileStream = webClientful.OpenRead("http://www.spiralfound.com/images/books/cover1.png"))
                        {
                            Bitmap bmp = ResizeImage(fileStream, 530, 360);
                            bmp.Save(Server.MapPath("~/images/books/" + newBookId + ".jpg"), ImageFormat.Jpeg);
                        }


                        var webClientthb = new WebClient();
                        using (var fileStream = webClientthb.OpenRead("http://www.spiralfound.com/images/books/bookthumb-cover1.png"))
                        {
                            Bitmap thb = ResizeImage(fileStream, 150, 100);
                            thb.Save(Server.MapPath("~/images/books/" + newBookId + "_s.jpg"), ImageFormat.Jpeg);
                        }
                    }
                    catch (SqlException ex)
                    {
                        ErrorMessage.Text = ex.Message;
                    }
                    finally
                    {
                        cmdinsert.Dispose();
                        if (con != null)
                        {
                            con.Close();
                        }
                    }

                    //// Copy Default User Image
                    //string oldPath = Server.MapPath("~/Images/Users/user.jpg");
                    //string newPath = Server.MapPath("~/Images/Users/" + sUserName + ".jpg");
                    //System.IO.File.Copy(oldPath, newPath);

                    var webClient = new WebClient();
                    using (var fileStream = webClient.OpenRead("https://graph.facebook.com/" + Session["fbUserId"].ToString() + "/picture?type=large"))
                    {
                        //Bitmap bmp = new Bitmap(fileStream);
                        Bitmap bmp = ResizeImage(fileStream, 180, 241);
                        bmp.Save(Server.MapPath("~/Images/Users/" + sUserName + ".jpg"), ImageFormat.Jpeg);
                    }


                    FacebookLoginHelper helper = new FacebookLoginHelper();
                    if (Request.Params.AllKeys.Contains("code"))
                    {
                        Dictionary <string, string> dicAccessToken = helper.GetAccessToken(Request["code"].ToString(), FacebookApp.Scope(), FacebookApp.RegRedirectUrl());

                        var accessToken = dicAccessToken["access_token"];
                        // Write Friend IDs from JSON
                        var     json = new WebClient().DownloadString("https://api.facebook.com/method/friends.getAppUsers?access_token=" + accessToken + "&format=json");
                        var     jss  = new JavaScriptSerializer();
                        dynamic data = jss.Deserialize <dynamic>(json);

                        con.Open();

                        foreach (dynamic friend in data)
                        {
                            SqlCommand cmdfollow = new SqlCommand();
                            cmdfollow.CommandType = CommandType.StoredProcedure;
                            cmdfollow.CommandText = "User_Follow_Facebook";
                            cmdfollow.Parameters.Add("@UserFbId", SqlDbType.VarChar, 100).Value   = Session["fbUserId"].ToString();
                            cmdfollow.Parameters.Add("@FriendFbId", SqlDbType.VarChar, 100).Value = friend;
                            cmdfollow.Connection = con;

                            cmdfollow.ExecuteNonQuery();
                            cmdfollow.Dispose();
                        }

                        if (con != null)
                        {
                            con.Close();
                        }
                    }

                    // Log User In
                    FormsAuthentication.SetAuthCookie(sUserName, false);
                    Response.Redirect("Default.aspx");
                }
            }
        }
コード例 #4
0
 public static string AuthenticationUrl()
 {
     return
         (string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", FacebookApp.ClientId(), HttpUtility.UrlEncode(FacebookApp.RedirectUrl()), FacebookApp.Scope()));
 }