Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get AccessToken on the Landing Page using Facebook C# SDK

Coding Platform ASP.NET 4.0 WebForms

I have two pages that are relevant here

  1. Login.aspx
  2. LandingPage.aspx

On Login.aspx when I click an ImageButton, I redirect to Facebook site with the following code

protected void FacebookLoginButton_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        Response.Redirect(GetFacebookLoginURL());
    }
    catch (System.Threading.ThreadAbortException)
    {
        throw;
    }
    catch (Exception err)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(err);
    }
}

private string GetFacebookLoginURL()
{
    try
    {
        string baseURL = System.Configuration
            .ConfigurationManager
            .AppSettings["WebsiteURL"]
            .ToString();

        string[] extendedPermissions = new[] { 
            "publish_stream", 
            "offline_access" 
        };

        var oauth = new FacebookOAuthClient { 
            ClientId = FacebookContext.Current.AppId 
        };

        var parameters = new Dictionary<string, object>{
            { "response_type", "token" },
            { "display", "page" }
        };

        if (extendedPermissions != null && extendedPermissions.Length > 0)
        {
            var scope = new StringBuilder();
            scope.Append(string.Join(",", extendedPermissions));

            parameters["scope"] = scope.ToString();
        }
        parameters["redirect_uri"] = String.Format("{0}LandingPage.aspx", baseURL);
        return oauth.GetLoginUrl(parameters).OriginalString;
    }
    catch (Exception err)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(err);
        return "";
    }
}

That part is working properly. But I am clueless on how to access the user info at the LandingPage which is my redirect_uri. Have tried this.

    FacebookOAuthClient cl = new FacebookOAuthClient(FacebookContext.Current);
    FacebookOAuthResult result = null; 
    string url = Request.Url.AbsoluteUri;

    // verify that there is a code in the url
    if (FacebookOAuthResult.TryParse(url, out result))
    {
        if (result.IsSuccess)                               
        {
            var accesstoken = result.AccessToken;
        }
        else
        {
            var errorDescription = result.ErrorDescription;
            var errorReason = result.ErrorReason;
        }
    }

But I doubt it wont work since I dont have window.hash.location at Server Side(Its not working anyway)

    var client = new FacebookClient(FacebookContext.Current);
    dynamic me = client.Get("me");
    string firstName = me.first_name;
    string lastName = me.last_name;
    string email = me.email;

Although I am not sure, getting the Access Token will solve my problem, won't it? This is the error I am getting.

(OAuthException) An active access token must be used to query information about the current user.

What am I doing wrong?

like image 685
naveen Avatar asked Nov 28 '25 03:11

naveen


1 Answers

here's a sample of a standalone website using webforms. Check out Default.aspx.cs and Web.config. Please note that this sample could be modified for use with the latest source code and might not work with the latest release (5.0.3 beta).

like image 73
jimmystormig Avatar answered Nov 30 '25 17:11

jimmystormig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!