Facebook Authorization (Facebook Login) And Access [Code] Querystring Parameter Value

Facebook authorization (facebook login) and access [code] querystring parameter value using facebook nuget package in C# application.
  1. Need to install "facebook" nuget package in c# project.
  2. Must create your Facebook app from https://developers.facebook.com/ and store appId and appSecret in your application.
  3. Execute below code for facebook authorization and call "FacebookLogin" method on action event. fig-1
    1. //fig-1  
    2. [HttpPost]  
    3. public string FacebookLogin() {  
    4.     try {  
    5.         // Instantiate the Facebook client  
    6.         var oauth = new FacebookClient();  
    7.         var fbLoginUrl = oauth.GetLoginUrl(new {  
    8.             client_id = "Enter ApplicationID",  
    9.                 client_secret = "Enter ApplicationSecret",  
    10.                 redirect_uri = "Enter RedirectUri",  
    11.                 response_type = "code",  
    12.                 scope = "manage_pages,email" // Add other permissions as needed  
    13.         });  
    14.         var fbloginUri = fbLoginUrl.AbsoluteUri;  
    15.         Session["ClientId"] = "Enter ApplicationID",  
    16.             return fbloginUri;  
    17.     } catch (Exception) {  
    18.         return null;  
    19.     }  
    20. // This is just a sample script. Paste your real code (javascript or HTML) here.  
    21. if ('this_is' == /an_example/) {  
    22.     of_beautifier();  
    23. else {  
    24.     var a = b ? (c % d) : e[f];  
  4. Here, you get below url for facebook login page,

    https://www.facebook.com/login.php?skip_api_login=?&api_key=?&signed_next=?&next=?&cancel_url=?&display=?&locale=?&logger_id=?

  5. After authoriing Facebook user, you get below url with [code] querystrings parameter:

    http://localhost:url?code=?

  6. You need to store [code] query string parameter value in session and call below function in page_load event. fig-2
    1. //fig-2  
    2. public void GetQueryStringValue() {  
    3.     string _code = string.Empty;  
    4.     if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.UrlReferrer.Query)) {  
    5.         List < string > collection = GetQueryStringCollection(System.Web.HttpContext.Current.Request.UrlReferrer.Query, 0);  
    6.         if (collection != null && collection.Count > 0) {  
    7.             _code = System.Web.HttpContext.Current.Server.UrlDecode(collection[0]);  
    8.             Session["code"] = _code;  
    9.         }  
    10.     }  
    11. }  
    12. // Get Querystring name value collection  
    13. public List < string > GetQueryStringCollection(string url, int count) {  
    14.     string keyValue = string.Empty;  
    15.     List < string > collection = new List < string > ();  
    16.     string[] querystrings = url.Split('&');  
    17.     if (querystrings != null && querystrings.Count() > count) {  
    18.         for (int i = 0; i < querystrings.Count(); i++) {  
    19.             string[] pair = querystrings[i].Split('=');  
    20.             collection.Add(pair[1]);  
    21.         }  
    22.     }  
    23.     return collection;  
    24. }  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now