问题
Can anyone point me in the direction of how I can use a form post with the Facebook C# SDK in an MVC app? It's an iframe based Canvas application.
I have the following code
[HttpPost, CanvasAuthorize(Permissions = "publish_stream")]
public ActionResult Enter(FormCollection col)
{
var fbApp = new FacebookClient(this.CurrentSession.AccessToken);
JsonObject result = (JsonObject)fbApp.Get("me");
long FacebookId = long.Parse(result["id"].ToString());
//CODE TO INSERT GOES HERE
return View();
}
And the form is this
<form action="/EnterComp/Enter" enctype="multipart/form-data" method="post">
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<label for="Text">Text</label>
</div>
<div class="editor-field">
<input id="Text" name="Text" type="text" value="" />
</div>
<div class="editor-label">
<label for="ImagePath">ImagePath</label>
</div>
<div class="editor-field">
<input type="file" name="ImagePath" id="ImagePath">
</div>
<div class="editor-label">
<label for="VideoPath">VideoPath</label>
</div>
<div class="editor-field">
<input type="file" name="VideoPath" id="VideoPath">
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
But everytime I submit the form, it appears to re-authenticate - and thus lose the form collection. Now I'm sure there's something really simple I'm missing, any help would be appreciated.
EDIT
I think I've answered my own question here, but in case anyone else after me looks. The solution, for me at least, was here.
However, if anyone can suggest anything else. Please help!
回答1:
There is actually a helper for MVC forms that will fix this issue for you.
The helper goes inside your form:
Razor Syntax:
@Html.FacebookSignedRequest();
ASP.NET Syntax
<%=Html.FacebookSignedRequest() %>
All this actually does is store your signed_requeset as a hidden on your form. This way when the form is posted the Facebook authenticator will be able to reauthorize the request.
来源:https://stackoverflow.com/questions/4960552/form-post-with-facebook-c-sharp-sdk