Using native Facebook App's authentication to login to Azure Mobileservice in Universal Windows App

I used the "Facebook authentication" in Azure Mobile App service but it requires user to enter Facebook credential to login by default, and so I need to get the user token from native Facebook app and then pass it to Azure App service. 

Below is the code I used to retrieve user token from native Facebook app. It will redirect user to native Facebook App to authenticate.

string fbAppId = "xxxxxxxxxx";
string fbWinAppId = "xxxxxxxxxxxxx";
string FacebookURL = "https://www.facebook.com/v2.7/dialog/oauth?client_id=" + fbAppId;
string callBackURL = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri;
string token = string.Empty;

FacebookURL += "&redirect_uri=" + System.Uri.EscapeDataString(callBackURL) +"&display=popup&response_type=token&state=1&scope=email";
 System.Uri startURI = new Uri(FacebookURL); System.Uri endURI = new Uri(callBackURL);
 var resultFB = await  Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startURI, endURI);         

// Handle the response from the Login Dialog if (resultFB.ResponseStatus == WebAuthenticationStatus.Success) {           string fbResult = resultFB.ResponseData.ToString();            fbResult = fbResult.Substring(fbResult.IndexOf("access_token"));
            String[] keyValPairs = fbResult.Split('&');
             for (int i = 0; i < keyValPairs.Length; i++)              {                      String[] splits = keyValPairs[i].Split('=');                       switch (splits[0])                       {                              case "access_token":                                    token = splits[1];                                     break;                            }                }  }

 // Login with the identity provider and access authon. JObject payload = new JObject(); payload.Add("access_token", token);
//Pass token to Azure Mobile service                 user = await App.MobileService.LoginAsync(provider, payload);
(Note: I tested in the desktop UWP and it will open the Facebook login page with my existing Facebook login credential to ask me to confirm. However, above approach not for windows phone as it will ask you to entry user name and password in Facebook login page. Leave your comments if you have better way to handle this.)

Comments

Popular posts from this blog

Top JavaScript courses helping you develop SPFx webPart with ReactJS and AngularJS

SharePoint 2013 error - "Application error when access /_vti_bin/client.svc"

Enable the Microsoft Power BI report file type (.pbix) in SharePoint Search