Login with facebook in asp.net using C#

Name Space

using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


   string appid = "";
  string secretid = "";


 // here replace "/Fb/Login.aspx"  with your redirect url page

protected void Page_Load(object sender, EventArgs e)
    {
   HyperLink1.NavigateUrl = "https://www.facebook.com/v2.4/dialog/oauth/?client_id=" + appid + "&redirect_uri=http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Fb/Login.aspx&response_type=code&state=1";
     

        if (Request.QueryString["code"] != "")
        {

            GetFacebookUserData(Request.QueryString["code"]);


        }
    }


  protected void GetFacebookUserData(string code) //List<Facebook.User>
    {
        // Exchange the code for an access token
        Uri targetUri = new Uri("https://graph.facebook.com/oauth/access_token?client_id=" + appid + "&client_secret=" + secretid + "&redirect_uri=http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Fb/Login.aspx&code=" + code);
        HttpWebRequest at = (HttpWebRequest)HttpWebRequest.Create(targetUri);

        System.IO.StreamReader str = new System.IO.StreamReader(at.GetResponse().GetResponseStream());
        string token = str.ReadToEnd().ToString().Replace("access_token=", "");

        // Split the access token and expiration from the single string
        string[] combined = token.Split('&');
        string accessToken = combined[0];

        // Exchange the code for an extended access token
        Uri eatTargetUri = new Uri("https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + appid + "&client_secret=" + secretid + "&fb_exchange_token=" + accessToken);
        HttpWebRequest eat = (HttpWebRequest)HttpWebRequest.Create(eatTargetUri);

        StreamReader eatStr = new StreamReader(eat.GetResponse().GetResponseStream());
        string eatToken = eatStr.ReadToEnd().ToString().Replace("access_token=", "");

        // Split the access token and expiration from the single string
        string[] eatWords = eatToken.Split('&');
        string extendedAccessToken = eatWords[0];





        Uri targetUserUri = new Uri("https://graph.facebook.com/me?fields=id,first_name,last_name,email,picture&access_token=" + accessToken);
        HttpWebRequest user = (HttpWebRequest)HttpWebRequest.Create(targetUserUri);

        // Read the returned JSON object response
        StreamReader userInfo = new StreamReader(user.GetResponse().GetResponseStream());
        string jsonResponse = string.Empty;
        jsonResponse = userInfo.ReadToEnd();

        // Deserialize and convert the JSON object to the Facebook.User object type
        JavaScriptSerializer sr = new JavaScriptSerializer();
        string jsondata = jsonResponse;
        // Response.Write(jsondata);


        JObject result = (JObject)JsonConvert.DeserializeObject(jsondata);

         Response.Write(result["id"]);
         Response.Write(result["first_name"]);
         Response.Write(result["last_name"]);
         Response.Write(result["picture"]["data"]["url"].ToString());



    }

Comments

Popular posts from this blog

Multipart/form-data using basic auth

How to Capture image of HTML5 camera in asp.net

Simple API that receive MultipartData File with parameter in ASP.net Web API