Facebook Login is a great way and very easy way for your user to connect to your website or to your client’s website. To make a Facebook Login button using ASP.net C# and only code behind calls you will need first to download the Facebook asp.net SDK, you can do so from the following link and add the Facebook.dll files as a reference in your project
- Add any button on the website and give it any image you want
- In the code behind add the following code to the on click event
string callbackUrl = “http://yourdomain.com/FacebookConnect.aspx”;
string appID = “yourFacebookAppID”;
//Request offline access and publish to the users stream access.
Responce.Redirect(” string.Format(“https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope=offline_access,email,publish_stream”, appID, callbackUrl)”);
- After redirecting to facebook the user will give you the requested access in “SCOPE” and will redirect back to the mentioned URL with a QueryString CODE
- Inside FacebookConnect.aspx add the following code
using Facebook; protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack) { HandleFacebookCallback(); } else { //Do something, the user didn't give you access; } } private void HandleFacebookCallback() { var oauthResult = Request.Params["code"]; string accessToken = GetOauthTokens(oauthResult)["access_token"].ToString(); if (string.IsNullOrEmpty(accessToken)) { // failed to get access token do something } var fb = new FacebookClient(accessToken); dynamic me = fb.Get("me"); // get all the user information string email = me.email; string facebookID = me.id; byte[] imageBytes = webClient.DownloadData(string.Format("https://graph.facebook.com/{0}/picture", me.id));
//perform login for the user } private Dictionary<string, string> GetOauthTokens(string code) { Dictionary<string, string> tokens = new Dictionary<string, string>(); string clientId = "facebook app ID" string redirectUrl = "http://yourdomain.com/FacebookConnect.aspx"; string clientSecret = "FacebookAppSecret"; string scope = "offline_access,email,publish_stream"; string url = string.Format("https://graph.facebook.com/oauth/access_token? client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}", clientId, redirectUrl, clientSecret, code, scope); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string retVal = reader.ReadToEnd(); foreach (string token in retVal.Split('&')) { tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1)); } } return tokens; }
Using the code mentioned above you should be able to get the information of the use and to make him login. This code worked great on one of the websites i made. If you see any error please drop me a message in the comment section below.
Thank you for sharing 🙂