Step 1
--------------------------------------------------------------------------------------------------------------------
First Create Your App ID on Following Link
http://developers.facebook.com/setup/
Site Name: Test Website
Site URL: http://localhost:8353/LSATWebsite/( for Local Test set your own local host like this is mine)
Site URL: www.mywebsite.com (for live Test)
click on create app
-----------------------------------------------------------------------------------------------------------------------
Step 2
// copy this code before Head tag
Step 3
// Set this login div where you want set your facebook login button
--------------------------------------------------------------------------------------------------------------------
First Create Your App ID on Following Link
http://developers.facebook.com/setup/
Site Name: Test Website
Site URL: http://localhost:8353/LSATWebsite/( for Local Test set your own local host like this is mine)
Site URL: www.mywebsite.com (for live Test)
click on create app
-----------------------------------------------------------------------------------------------------------------------
Step 2
// copy this code before Head tag
Step 3
// Set this login div where you want set your facebook login button
------------------------------------------------------------------------------------------------------------------------
Step 4
Index Page Code
page load()
{
FacebookLogin();
}
#region FacebookLogin
private void FacebookLogin()
{
if (Request.QueryString["loginby"] != null)
{
if (Convert.ToString(Request.QueryString["loginby"]) == "fb")
{
String AppID = "fbs_247350651959904";
if (Request.Cookies[AppID] != null)
{
try
{
Session["UserID"] = null;
string token = Request.Cookies[AppID].Values[0].ToString();
string userid = Request.Cookies[AppID].Values[5].ToString();
int charCount = (userid.Length - 1);
string finalUser = userid.Substring(0, charCount);
string fbUser = "https://graph.facebook.com/" + finalUser + "?access_token=" + token;
//Parse the JSON that is returned:
JObject u = JObject.Parse(Convert.ToString(requestFBData(fbUser)));
string userName = "";
if (u["link"].ToString().Contains("profile.php?id="))
userName = u["link"].ToString().Replace("http://www.facebook.com/profile.php?id=", "").Replace("\"", "");
else
userName = u["link"].ToString().Replace("http://www.facebook.com/", "").Replace("\"", "");
// Now get their email address and first name (we will need it later).
UserClass objUser = new UserClass();
//objUser.Email = u["email"].ToString().Replace("\"", "");
// objUser.password = Membership.GeneratePassword(10, 6);
objUser.FacebookToken = token;
objUser.FaceBookId = userName;
objUser.Name = u["first_name"].ToString().Replace("\"", "");
//objUser.lstname = u["last_name"].ToString().Replace("\"", "");
objUser.OutputMessage = "http://graph.facebook.com/" + finalUser + "/picture";
//string userFacebook = u["link"].ToString().Replace("\"", "");
Session["UserID"] = objUser.LoginByFacebookToken();
if (Session["UserID"] != null && Session["UserID"] != "")
{
objUser.UserID = Convert.ToInt32(Session["UserID"]);
Session["FirstName"] = "Hi, " + objUser.Get_Username() + " Good Luck With Your Lsat.";
Session["FbLogin"] = "True";
}
FormsAuthentication.SetAuthCookie(userName, true);
Response.Redirect("index.aspx", true);
}
catch { Exception e; }
}
}
}
}
object requestFBData(string action)
{
WebRequest req = HttpWebRequest.Create(action);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
return results;
}
#endregion
// i am using user class to save data on my databse
------------------------------------------------------------------------------------------------------------------------
Step 6
Log Out Page Code
page load ()
{
LogOut();
}
private void LogOut()
{
if (Request.QueryString["logout"] != null)
{
if (Convert.ToString(Request.QueryString["logout"]) == "fb")
{
String AppID = "fbs_247350651959904";
if (Request.Cookies[AppID] == null)
{
Session.Abandon();
Response.Redirect("index.aspx");
}
}
}
}
No comments:
Post a Comment