-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfbchannel.ashx.pp
104 lines (98 loc) · 5.43 KB
/
fbchannel.ashx.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//-----------------------------------------------------------------------
// <copyright file="fbchannel.ashx.pp" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author>
// <website>https://github.com/facebook-csharp-sdk/facebook-aspnet-channel-file</website>
//-----------------------------------------------------------------------
// Install-Package Facebook.channel
<%@ WebHandler Language="C#" Class="$rootnamespace$.FacebookChannelHandler" %>
namespace $rootnamespace$
{
using System;
using System.Web;
/// <summary>
/// Channel file for Facebook to address issues with cross domain communication in certain browsers.
/// </summary>
/// <remarks>
/// http://developers.facebook.com/docs/reference/javascript
///
/// The channel file addresses some issues with cross domain communication in certain browsers.
/// The contents of the channel.html file can be just a single line:
///
/// <script src="//connect.facebook.net/en_US/all.js"></script>
///
/// It is important for the channel file to be cached for as long as possible.
/// When serving this file, you must send valid Expires headers with a long expiration period.
/// This will ensure the channel file is cached by the browser which is important for a smooth user experience.
/// Without proper caching, cross domain communication will become very slow and users will suffer a severely
/// degraded experience.
///
/// The channelUrl parameter is optional, but recommended.
/// Providing a channel file can help address three specific known issues.
/// First, pages that include code to communicate across frames may cause Social Plugins to show up as blank
/// without a channelUrl. Second, if no channelUrl is provided and a page includes auto-playing audio or video,
/// the user may hear two streams of audio because the page has been loaded a second time in the background for
/// cross domain communication. Third, a channel file will prevent inclusion of extra hits in your server-side logs.
/// If you do not specify a channelUrl, you can remove page views containing fb_xd_bust or fb_xd_fragment parameters
/// from your logs to ensure proper counts.
///
/// The channelUrl must be a fully qualified URL matching the page on which you include the SDK.
/// In other words, the channel file domain must include www if your site is served using www,
/// and if you modify document.domain on your page you must make the same document.domain change in the channel.html
/// file as well. The protocols must also match. If your page is served over https, your channelUrl must also be https.
/// Remember to use the matching protocol for the script src as well. The sample code above uses protocol-relative
/// URLs which should handle most https cases properly.
///
/// <div id="fb-root"></div>
/// <script>
/// window.fbAsyncInit = function() {
/// FB.init({
/// appId : 'YOUR_APP_ID', // App ID
/// channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
/// status : true, // check login status
/// cookie : true, // enable cookies to allow the server to access the session
/// xfbml : true // parse XFBML
/// });
///
/// // Additional initialization code here
/// };
///
/// // Load the SDK Asynchronously
/// (function(d){
/// var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
/// if (d.getElementById(id)) {return;}
/// js = d.createElement('script'); js.id = id; js.async = true;
/// js.src = "//connect.facebook.net/en_US/all.js";
/// ref.parentNode.insertBefore(js, ref);
/// }(document));
/// </script>
///
/// </remarks>
public class FacebookChannelHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
response.ClearHeaders();
const int cacheExpires = 60 * 60 * 24 * 365;
response.AppendHeader("Pragma", "public");
response.AppendHeader("Cache-Control", "maxage=" + cacheExpires);
response.AppendHeader("Expires", DateTime.Now.ToUniversalTime().AddSeconds(cacheExpires).ToString("r"));
context.Response.ContentType = "text/html";
context.Response.Write("<script src=\"//connect.facebook.net/en_US/all.js\"></script>");
}
public bool IsReusable { get { return false; } }
}
}