问题
I'm using build 5.06 of the toolkit and I'm just getting started, trying to do something simple: get a postback to work. But when the user clicks the postback button, the browser is redirected to something quickly (I think it's uiserver.???) and then redirected again to facebookredirect.axd, and finally again to my home canvas page. So, the entire session is pretty much reset. I'm hoping there's something obvious that I'm missing (like I should not call authorize on the postback?). Here's my code:
using System; using System.Configuration; using System.Web.UI; using Facebook; using Facebook.Web;
public partial class _Default : Page {
/// <summary>
/// Gets the current canvas facebook session.
/// </summary>
public FacebookSession CurrentSession
{
get { return (new CanvasAuthorizer()).Session; }
}
protected void Page_Load(object sender, EventArgs e)
{
var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me" } };
if (auth.Authorize())
{
ShowFacebookContent();
}
}
private void ShowFacebookContent()
{
var fb = new FacebookClient(this.CurrentSession.AccessToken);
dynamic myInfo = fb.Get("me");
lblName.Text = myInfo.name;
lblName.Text += myInfo.gender;
pnlHello.Visible = true;
}
protected void TempSaveButton_Click(object sender, EventArgs e)
{
var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me" } };
if (auth.Authorize())
{
var fb = new FacebookClient(this.CurrentSession.AccessToken);
dynamic myInfo = fb.Get("me");
lblDisplayText.Text = "Was authorized! name is" + myInfo.name;
}
else
{
lblDisplayText.Text = "Was not authorized!";
}
}
}
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label runat="server" ID="a" />
<asp:Panel ID="pnlHello" runat="server" Visible="false"> </asp:Panel>
<h2>
Hello
<asp:Label ID="lblName" runat="server" />!
</h2>
<div>Hello ! Please press button to do postback.
<asp:Button ID="TempSaveButton" Text="Temp Save" runat="server" onclick="TempSaveButton_Click"></asp:button>
<asp:Panel ID="pnlError" runat="server" Visible="false">
<a href="Default.aspx">
<asp:Label ID="lblDisplayText" runat="server" ForeColor="Red" /><br />
</a>
</asp:Panel>
(in the above, the label does not get set at all after the postback.
Thanks for any help...
-Ben
回答1:
You will need to maintain the signed_request manually for postbacks and ajax requests.
Try creating a hidden input field which contains the signed_request.
<input type="hidden" name="signed_request" value="<%: Request.Params["signed_request"]%>"/>
Name the hidden field signed_request. So when you type the following code, access_token is automatically set.
var fb = new FacebookWebClient();
For mvc there is a helper method for this.
@Html.FacebookSignedRequest()
来源:https://stackoverflow.com/questions/5357728/asp-net4-canvas-app-postback-causes-unexpected-redirect