Monday, March 26, 2018

Sign in with Facebook without using Third party library C#

  No comments
March 26, 2018



Hello everyone today I would like to share a post on facebook authentication, I have wondered facebook Authentication on internet material none of found without using any third party app so I hope this may help you to direct your application.


First step is to go to Facebook developer

> Create App
> Add Display Name
> Add.

It will redirect you to the dashboard of an app.

Set up for Facebook Login.
Choose Web as a platform
Add project details.

update policy & term URL in an app form and in product > Facebook Login Update Client OAuth Settings Enable
>Client OAuth Login
>Web OAuth Login
>Login from Devices

Valid OAuth Redirect URIs
Update valid redirect URL this will be redirected once user authorizes an app.

Deauthorize Callback URL
Update for redirecting user if denied authorization and save changes.

Go to the visual studio create project Add page and create a button on click event add function.

window.location = "https://www.facebook.com/v2.12/dialog/oauth?client_id=AppID
&display=popup&response_type=token&redirect_uri=yourRedirectURI
&scope=public_profile,email,user_hometown,user_location,user_work_history,user_birthday"

Update your App Id, Redirect URI and scope details you needed. All scope is defined here

Once the user authorizes the app it will redirect back to our page with access token from facebook.
To retrieve an access token from URL add below method

var access_token = "";
var urlcheck = window.location.href;
access_token = urlcheck.match(/\#(?:access_token)\=([\S\s]*?)\&/)[1];

once user get request send the token to a c# method

public static async Task Get(string token)
{
var HttpClient _httpClient= new HttpClient();
var url=@"https://graph.facebook.com/v2.12/me?
fields=id,name,picture.width(100).height(100).as(picture_small),picture.width(720).height(720).as(picture_large)&access_token="+token;
using (var result = await _httpClient.GetAsync(url))
{
string content = await result.Content.ReadAsStringAsync();
return content;
}
}

In C# method update all details you need to retrieve. and call an async method. In content data will display in an array, That's sit All details now you have you can serialize data if you use to set on the property.

Read More