-1

I am currently working on facebook sharing and it seems there aren't many topics on facebook sharing with C#. Trying to learn something...

I have Open Graph meta tags in one of razor views like below:

<head>
    <meta name="viewport" content="width=device-width" />
    <title>Hello World!</title>
    <meta property="fb:app_id" content="************" />
    <meta property="og:site_name" content="www.hello-world.com" />
    <meta property="og:type" content="website" />
    <meta property="og:url" content="http://hello-world.com/home/fbshare" />
    <meta property="og:title" content="How are you doing today?" />
    <meta property="og:description" content="Great to know you are doing fine." />
    <meta name="author" content="Hello" />
    <meta property="og:image" content="https://images.pexels.com/photos/2324/skyline-buildings-new-york-skyscrapers.jpg?w=940&h=650&auto=compress&cs=tinysrgb" />
    <meta property="og:image:type" content="image/jpeg" />
    <meta property="og:image:width" content="800" />
    <meta property="og:image:height" content="420" />
</head>

For normal users I'd like to redirect them to somewhere else from this view like below:

@{Response.Redirect("Somewhere in my application")};

But I do not want to redirect the facebook scraper too. Is there anyway I can identify the facebook or facebook scraper user agent and If I found out that this is facebook user agent do not redirect him anywhere else redirect them from the view to somewhere else.

P.S: According to facebook official documentation I have to find facebook user agent and allow them scrape my Open Graph meta tags but I can't dig a way.

The Facebook crawler needs to be able to access your content in order to scrape and share it correctly. Your pages should be visible to the crawler. If you require login or otherwise restrict access to your content, you'll need to whitelist our crawler. You should also exempt it from DDoS protection mechanisms.

If content isn't available at the time of scraping, you can force a rescrape once it becomes available by passing the URL through the Sharing Debugger.

The Facebook crawler can be identified by either of these user agent strings:

facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php) OR facebookexternalhit/1.1

Newbie
  • 47
  • 1
  • 7

1 Answers1

0

Have you tried:

var userAgent = HttpContext.Request.Headers["User-Agent"];
if (userAgent != null && 
    userAgent.Contains("facebookexternalhit/"))
{
    // Is a Facebook agent
}
else
{
    // Is not a Facebook agent
}

Of course, in MVC you should never redirect from a view, only redirect from a controller or a filter.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • No luck, It still redirects the scrapper to the action I define for redirect. Any thought? – Newbie May 29 '17 at 18:51
  • I changed my answer based on what was [here](https://stackoverflow.com/a/9031912/181087). It is possible that there is more information in the user agent string than what is passed from the actual agent. You should setup the code to log what is actually in the header so you can subsequently setup the condition right. – NightOwl888 May 29 '17 at 19:33
  • I tried to log the user agent but actually it is null. The text file that logs are saved in it is null and shows nothing. what could be the problem? – Newbie May 29 '17 at 20:48
  • Are you actually testing it using the Facebook crawler? Of course it will be null if it is a normal request, which is what the else condition is for above. You need to test what the Facebook crawler actually does and then create a request with the same header in order to test the above code. – NightOwl888 May 29 '17 at 20:49
  • Sorry it was because of the permission stuff. After I did give permission facebook to write on file I get this `facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)` user agent. even putting this user agent didn't helped. It still redirects... Silly me – Newbie May 29 '17 at 21:01
  • I need your help, please help me out! – Newbie May 30 '17 at 07:28