Form Post with Facebook C# SDK

╄→гoц情女王★ 提交于 2019-11-28 05:48:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!