Facebook authorization (facebook login) and access [code] querystring parameter value using facebook nuget package in C# application.
- Need to install "facebook" nuget package in c# project.
- Must create your Facebook app from https://developers.facebook.com/ and store appId and appSecret in your application.
- Execute below code for facebook authorization and call "FacebookLogin" method on action event. fig-1
-
- [HttpPost]
- public string FacebookLogin() {
- try {
-
- var oauth = new FacebookClient();
- var fbLoginUrl = oauth.GetLoginUrl(new {
- client_id = "Enter ApplicationID",
- client_secret = "Enter ApplicationSecret",
- redirect_uri = "Enter RedirectUri",
- response_type = "code",
- scope = "manage_pages,email"
- });
- var fbloginUri = fbLoginUrl.AbsoluteUri;
- Session["ClientId"] = "Enter ApplicationID",
- return fbloginUri;
- } catch (Exception) {
- return null;
- }
- }
- if ('this_is' == /an_example/) {
- of_beautifier();
- } else {
- var a = b ? (c % d) : e[f];
- }
- 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=?
- After authoriing Facebook user, you get below url with [code] querystrings parameter:
http://localhost:url?code=?
- You need to store [code] query string parameter value in session and call below function in page_load event. fig-2
-
- public void GetQueryStringValue() {
- string _code = string.Empty;
- if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.UrlReferrer.Query)) {
- List < string > collection = GetQueryStringCollection(System.Web.HttpContext.Current.Request.UrlReferrer.Query, 0);
- if (collection != null && collection.Count > 0) {
- _code = System.Web.HttpContext.Current.Server.UrlDecode(collection[0]);
- Session["code"] = _code;
- }
- }
- }
-
- public List < string > GetQueryStringCollection(string url, int count) {
- string keyValue = string.Empty;
- List < string > collection = new List < string > ();
- string[] querystrings = url.Split('&');
- if (querystrings != null && querystrings.Count() > count) {
- for (int i = 0; i < querystrings.Count(); i++) {
- string[] pair = querystrings[i].Split('=');
- collection.Add(pair[1]);
- }
- }
- return collection;
- }