This article demonstrates how to post content (status, image, video, URL) to a user's Facebook wall using ASP.Net. Since I was thinking of implementing this feature in my current project, I found something cool and decided to share it with C# Corner readers.
Getting Started
First of all create a new website in ASP.Net using C#. And now login into Facebook and click on the developer link and create a new app and supply an app name and click "Continue".
Image1.
Now supply the captcha code in the TextBox and click "Continue".
Image 2.
As you can see, there is an AppID and App Secret. You must use these so copy them. And supply the site URL and click "Save changes".
Image 3.
Now let's start working on the UI. In this sample I will explain two ways to post a feed.
First scenario
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
- <div id="fb-root">
- </div>
- <script>
- window.fbAsyncInit = function () {
- FB.init({ appId: '491677990891136', status: true, cookie: true,
- xfbml: true
- });
- };
- (function () {
- var e = document.createElement('script'); e.async = true;
- e.src = document.location.protocol +
- '//connect.facebook.net/en_US/all.js';
- document.getElementById('fb-root').appendChild(e);
- } ());
- </script>
- <script type="text/javascript">
- $(document).ready(function () {
- $('#share_button').click(function (e) {
- e.preventDefault();
- FB.ui(
- {
- method: 'feed',
- name: 'jQuery Dialog',
- link: 'http://www.c-sharpcorner.com/authors/raj1979/raj-kumar.aspx',
- picture: 'http://www.c-sharpcorner.com/UploadFile/AuthorImage/raj1979.gif',
- caption: 'This article demonstrates how to use the jQuery dialog feature in ASP.Net.',
- description: 'First of all make a new website in ASP.Net and add a new stylesheet and add .js files and put images in the images folder and make a reference to the page.',
- message: 'hello raj message'
- });
- });
- });
- </script>
- <div>
- <input type="button" id="share_button" value="Share" />
- </div>

Image 4.
Type a message and click "Share".

Image 5.
Login to Facebook and see wall posts.
Second scenario
Add the Facebook client assembly in the bin folder and work on the code behind.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net;
- using System.IO;
- using Facebook;
- public partial class Default2 : System.Web.UI.Page
- {
- string bodymessage = "This is test message description new";
- protected void Page_Load(object sender, EventArgs e)
- {
- CheckAuthorization();
- }
- private void CheckAuthorization()
- {
- string app_id = "APP_ID";
- string app_secret = "APP_SECRET";
- string scope = "publish_stream,manage_pages";
- if (Request["code"] == null)
- {
- Response.Redirect(string.Format(
- "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
- app_id, Request.Url.AbsoluteUri, scope));
- }
- else
- {
- Dictionary<string, string> tokens = new Dictionary<string, string>();
- string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
- app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
- HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string vals = reader.ReadToEnd();
- foreach (string token in vals.Split('&'))
- {
- tokens.Add(token.Substring(0, token.IndexOf("=")),
- token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
- }
- }
- string access_token = tokens["access_token"];
- var client = new FacebookClient(access_token);
- client.Post("/me/feed", new { message = bodymessage });
- }
- }
- }
Run the application to see the result.
Image 6.


Younes SalehiPosted Apr 13, 2022, 6:34 AM EDT
Hello. Thanks for the very useful article. I could not find Facebook Client in this page. May I ask you to share it with me at [email protected]? Thanks again.
immad uddinPosted Jul 3, 2020, 4:19 AM EDT
Invalid Scopes: publish_stream, manage_pages. This message is only shown to developers. Users of your app will ignore these permissions if present. Please read the documentation for valid permissions at: https://developers.facebook.com/docs/facebook-login/permissions
Dulce Ávila MendozaPosted Jul 22, 2019, 8:14 AM EDT
Any idea to upload your profile and like in your profile picture?
Sweetu PatelPosted Jun 19, 2018, 9:13 PM EDT
Hello, can you please send me the source code on [email protected]
Anu VPosted Jul 12, 2016, 1:09 AM EDT
Nice
Venkatesh PanugantiPosted Jul 5, 2016, 4:00 AM EDT
Nice article <a href="https://www.allinterviewquestions.in/">.net interview questions</a>
Ravi ShahPosted Dec 3, 2015, 4:13 AM EST
hi.. Nice article how to get Request["code"] in C# time.. it display null in every time..
Upendra Pratap ShahiPosted Aug 21, 2015, 3:13 AM EDT
nice one
Malik AsgharPosted Jul 16, 2014, 1:07 AM EDT
hi raj i want to add my custom text in say something box ,how it possible
Bhavesh DobariyaPosted Apr 29, 2014, 3:56 AM EDT
what is FacebookClient?? from where it comes???
Trung NguyenPosted Sep 18, 2013, 10:45 PM EDT
Please send me the source code to [email protected]
satya dineshPosted Aug 18, 2013, 4:27 AM EDT
Please send me the source code to [email protected]
Raj KumarTOP 100Posted May 11, 2013, 7:57 AM EDT
u can install facebook assembly from NuGet.
Rajeev RanjanPosted May 10, 2013, 1:11 AM EDT
sir from where i will get the Facebook client assembly ??
Suthish NairTOP 100Posted Apr 8, 2013, 12:00 AM EDT
Hi abul, everything is given here.. why you just get the samples and try yourself..
abul hasanPosted Apr 4, 2013, 4:35 AM EDT
Hi Dude this good articles can u give demo for this or plz send demo for this to [email protected]
Ashutosh ChaturvediPosted Mar 17, 2013, 8:18 AM EDT
It's really good ...
Yehya ZreikeditedPosted Mar 10, 2013, 1:26 PM EDTEdited Mar 10, 2013, 2:22 PM EDT
Thank you, I Tried the ASP.NET scenario & it is working very well
Raj KumarTOP 100Posted Mar 7, 2013, 8:11 PM EST
@Suthish Nair I can give you my password :)
Raj KumarTOP 100Posted Mar 7, 2013, 8:10 PM EST
@Yehka and Carla this is the only source code. main thing is configure on facebook. private void CheckAuthorization()
Yehya ZreikPosted Mar 6, 2013, 12:50 PM EST
would you please provide us with the source code
carlaPosted Mar 5, 2013, 8:39 PM EST
No source code?
Rajeev KumarPosted Feb 28, 2013, 11:48 PM EST
gud... sure i will try to implement it.
Suthish NairTOP 100Posted Feb 28, 2013, 10:56 PM EST
good one.. password is secret :)
Neha SharmaPosted Feb 28, 2013, 10:19 PM EST
intresting..