I have some authentication problem in my app.. its http://apps.facebook.com/exmpldatetime I'm using old sdk (v2.0 ) n fbconnect class bt having a problem..
- user logins.
- goes to my app.
- canvas page shows connect to facebook login button.
- if user clicks this login button or refresh the page it shows hi,username (and this should be the actual case)
- now if user goes to home page of account and then goes to app now it dont shows the login button just shows hi,username
Why this all is happening?
here is the code for asp page
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using facebook;
using facebook.web;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
API api = new API();
fbConnect fbconnect = new fbConnect();
if (fbConnect.isConnected()) // Check if this application is authenticated
{
api.ApplicationKey = fbConnect.ApiKey;
api.SessionKey = fbConnect.SessionKey;
api.Secret = fbConnect.SecretKey;
api.uid = fbConnect.UserID;
// hide the facebook button
pnlLogin.Visible = false;
//Get the data of the signed user.
string fullName = "<img src=\"" + api.users.getInfo(fbConnect.UserID).pic_small + "\" /> " + " " + " " + "Hi," + " " + api.user开发者_如何转开发s.getInfo(fbConnect.UserID).first_name.ToString() + " " + api.users.getInfo(fbConnect.UserID).last_name.ToString();
lblMessage.Text = fullName;
}
else
{
pnlLogin.Visible = true;
}
}
}
html code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Canvas.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en">
<head id="Head1" runat="server">
<title>CanvasPage</title>
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"
type="text/javascript"></script>
</head>
<body >
<form id="form1" runat="server" >
<div id="login" class="login">
<asp:Panel ID="pnlLogin" runat="server">
<fb:login-button length="long" onlogin="window.location.reload()">
</fb:login-button>
</asp:Panel>
<table>
<tr>
<td>
<asp:Label ID="lblMessage" runat="server" Text="Not authenticated. Click Button to authenticate"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
FB.init("API Key", "xd_receiver.htm");
</script>
</body>
</html>
code for fbconnect class
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using facebook;
using facebook.Utility;
public class fbConnect
{
public fbConnect()
{}
public static bool isConnected()
{
API api;
api = new API();
api.SessionKey = SessionKey;
api.ApplicationKey = ApiKey;
api.Secret = SecretKey;
api.uid = UserID;
if (api.SessionKey == null)
{
return false;
}
// can't really tell if the session key we have is still valid
try
{
api.uid = api.users.getLoggedInUser();
}
catch (FacebookException)
{
// invalid session key found
ClearFacebookCookies();
return false;
}
return true;
}
public static string ApiKey
{
get
{ return ConfigurationManager.AppSettings["APIKey"];}
}
public static string SecretKey
{
get
{return ConfigurationManager.AppSettings["Secret"];}
}
public static string SessionKey
{
get
{return GetFacebookCookie("session_key"); }
}
public static void ClearFacebookCookies()
{
string[] cookies = new[] { "user", "session_key", "expires", "ss" };
foreach (var c in cookies)
{
string fullCookie = ApiKey + "_" + c;
if (HttpContext.Current != null &&
HttpContext.Current.Response.Cookies[fullCookie] != null)
{ HttpContext.Current.Response.Cookies[fullCookie].Expires = DateTime.Now.AddMonths(-2); }
}
}
public static long UserID
{
get
{
long userID = -1;
long.TryParse(GetFacebookCookie("user"), out userID);
return userID;
}
}
private static string GetFacebookCookie(string cookieName)
{
string retString = null;
string fullCookie = ApiKey + "_" + cookieName;
if (HttpContext.Current.Request.Cookies[fullCookie] != null)
retString = HttpContext.Current.Request.Cookies[fullCookie].Value;
return retString;
}
}
Your facebook authentication happens at the client side and fbConnect.isConnected() is at the server side.
If you shift your fbConnect.isConnected() to javascript at the client side, I think should solve the issue.
精彩评论