0

I already create My Apps in https://developers.facebook.com and I already get my app id and app secret. which i already set to my

client_id = "I hide it first", client_secret = "I hide it first",

My question is when I run my application their is no result found to my cshtml page.

loginController :

private Uri RediredtUri

{

    get

    {

        var uriBuilder = new UriBuilder(Request.Url);

        uriBuilder.Query = null;

        uriBuilder.Fragment = null;

        uriBuilder.Path = Url.Action("FacebookCallback");

        return uriBuilder.Uri;

    }

}




[AllowAnonymous]

public ActionResult Facebook()

{

    var fb = new FacebookClient();

    var loginUrl = fb.GetLoginUrl(new

    {

        client_id = "I hide it first",

        client_secret = "I hide it first",

        redirect_uri= RediredtUri.AbsoluteUri,

        response_type="code",

        scope="email"
    });

    return Redirect(loginUrl.AbsoluteUri);

}




public ActionResult FacebookCallback(string code)

{

    var fb = new FacebookClient();

    dynamic result = fb.Post("oauth/access_token", new

    {

        client_id = "I hide it first",

        client_secret = "I hide it first",

        redirect_uri = RediredtUri.AbsoluteUri,

        code = code

    });

    var accessToken = result.access_token;

    Session["AccessToken"] = accessToken;

    fb.AccessToken = accessToken;

    dynamic me = fb.Get("me?fields=link,first_name,currency,last_name,email,gender,locale,timezone,verified,picture,age_range");

    string email = me.email;

    TempData["email"] = me.email;

    TempData["first_name"] = me.first_name;

    TempData["lastname"] = me.last_name;

    TempData["picture"] = me.picture.data.url;

    FormsAuthentication.SetAuthCookie(email, false);

    return RedirectToAction("Login", "login");

}

Login.cshtml :

@Html.ActionLink("Login with facebook", "Facebook", "login")

<table>

    <tr><td>Email:</td><td><b>@TempData["email"]</b></td></tr>

    <tr><td>First Name:</td><td><b>@TempData["first_name"]</b></td></tr>



    <tr><td>Last Name:</td><td><b>@TempData["lastname"]</b></td></tr>

    <tr><td>Picture:</td><td><b><img src="@TempData["picture"]" /></b></td></tr>



</table>

I have sample image. Sample Image

Dev George
  • 49
  • 13

1 Answers1

0

Seems you're using FB authentication with SDK similar like this example. If you're sure that me.email contains email data and you get empty values after redirection (note that I found you're using TempData to show results in login view page), possibly you need to keep TempData contents before redirection using TempDataDictionary.Keep() method:

public ActionResult FacebookCallback(string code)
{
    var fb = new FacebookClient();
    dynamic result = fb.Post("oauth/access_token", new
    {
        client_id = "your_app_id",
        client_secret = "your_app_secret_key",
        redirect_uri = RediredtUri.AbsoluteUri,
        code = code

    });

    var accessToken = result.access_token;
    Session["AccessToken"] = accessToken;

    fb.AccessToken = accessToken;
    dynamic me = fb.Get("me?fields=link,first_name,currency,last_name,email,gender,locale,timezone,verified,picture,age_range");

    string email = me.email;

    TempData["email"] = me.email;

    TempData["first_name"] = me.first_name;

    TempData["lastname"] = me.last_name;

    TempData["picture"] = me.picture.data.url;

    // add this line to keep TempData values when passing to view
    TempData.Keep();

    FormsAuthentication.SetAuthCookie(email, false);
    return RedirectToAction("Login", "login");
}

The reason behind TempDataDictionary.Keep() usage can be found here.

As a side note, better to use strongly-typed viewmodel properties and assign all values stored at TempData into them to avoid losing TempData contents when passing to view:

Model

public class FBUser
{    
    public string Email { get; set; }

    // other properties
}

Controller Action (Login)

var model = new FBUser();

model.Email = TempData["email"];
// other properties assignment

// other stuff

return View(model);

View

@model FBUser

@Html.ActionLink("Login with Facebook", "Facebook", "login")

<table>
    <tr>
       <td>Email:</td>
       <td>
          <b>@Html.LabelFor(m => m.Email)</b>
       </td>
    </tr>

    <!-- other properties -->

</table>

<!-- other stuff -->
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61