Question

I've just created a new facebook app, and all works fine, except controller actions which handle the "post" from my app, with [FacebookAuthorize] filter on action; since it creates a redirect and all of my form data is lost. You can see what I am talking about if you modify the only form presented in this tutorial:

http://www.asp.net/mvc/tutorials/mvc-4/aspnet-mvc-facebook-birthday-app

<form class="navbar-form pull-right" action="@Url.Action("Search", "Home")" 
method="get">
                  <input class="span2" type="text" name="friendName" placeholder="Friend's name" />
                  <button type="submit" class="btn">Search</button>
                </form>

If I change form's method to "post", the parameter in my Search method in controller is no longer filled. I did some research on this, and it seems that FacebookAuthorize filter creates a redirect and loses all post-data. How to make a workaround, or am I doing something wrong?

The action is here:

[FacebookAuthorize]
    [HttpPost]
    public async Task<ActionResult> Search(string friendName, FacebookContext context)
    {
        return null;
    }

When I place a breakpoint inside, I can see that parameter friendName is empty.

Était-ce utile?

La solution

I found an answer. The key is to add a hidden field named "signed_request" and fill it with the same value from request:

vm.signed_request = Request["signed_request"];

Why is that so? Here lies the answer: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/Microsoft.AspNet.Mvc.Facebook/Authorization/FacebookAuthorizeFilter.cs

FacebookAuthorizeFilter checks that particular field and makes a redirect if field is not filled.

I've described it further here and introduced useful Html helper for that purpose: My blog post

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top