protected void Page_Load(object sender, EventArgs e)
        {
            string url = "";
            oAuthFacebook oAuth = new oAuthFacebook();

            if (Request["code"] == null)
            {
                //Redirect the user back to Facebook for authorization.
                Response.Redirect(oAuth.AuthorizationLinkGet());
            }
            else
            {
                //Get the access token and secret.
                oAuth.AccessTokenGet(Request["code"]);

                if (oAuth.Token.Length > 0)
                {
                    //We now have the credentials, so we can start making API calls
                    url = "https://graph.facebook.com/me?access_token=" + oAuth.Token;
                    string data = "";
                    string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, data);
                    json = oAuth.convert(json);

                    var c = JsonConvert.DeserializeObject<Perfil>(json);

                    string primeiro_nome = c.first_name;
                    string ultimo_nome = c.last_name;
                    string pais = c.locale;
                    string facebook = c.link;
                    string username = c.username;
                    string mail = c.username + "@facebook.com";
                    string password = Path.GetRandomFileName();
                    password = password.Replace(".", "");
                    bool success=Users.InsereUtilizador(username, password, mail , primeiro_nome, ultimo_nome, "", pais);

                    // Gmail Address from where you send the mail
                    string fromAddress = "*****@*****.**";
                    // any address where the email will be sending
                    const string fromPassword = "******";
                    string toAddress = mail;
                    // Passing the values and make a email formate to display
                    string body="";
                    if (success)
                    {
                         body= "Obrigado por se registar! \n";
                        body += "Utilizador: " + username + "\n";
                        body += "Password: "******"\n\n";
                        body += "Já pode fazer login e editar a sua informação pessoal! http://wvm054.dei.isep.ipp.pt\n";
                    }
                    else
                    {
                        body = "Esta conta ja está em uso! Registo falhou! \n";
                        body += "Para recuperar a conta fale com um administrador! \n";
                    }
                    string subject = "Registo pelo Facebook";

                    // smtp settings
                    var smtp = new System.Net.Mail.SmtpClient();
                    {
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                        smtp.Timeout = 20000;
                    }
                    // Passing values to smtp object
                    smtp.Send(fromAddress, toAddress, subject, body);
                    Response.AddHeader("REFRESH", "5;URL=Login.aspx");
                }
            }
        }
示例#2
0
 protected void Button2_Click(object sender, EventArgs e)
 {
     oAuthFacebook oFB = new oAuthFacebook();
     Response.Redirect(oFB.AuthorizationLinkGet());
 }