My favorites | Sign in
Project Logo
       
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Collections.Generic;
using System.Text;
using Facebook.Components;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace Facebook.MvcWebControls
{
internal class BasePageHelper
{
#region Constants

private const string SESSION_SESSION_KEY = "SessionKey";
private const string SESSION_USER_ID = "UserId";
private const string REQUEST_IN_CANVAS = "fb_sig_in_canvas";
private const string REQUEST_SESSION_KEY = "fb_sig_session_key";
private const string REQUEST_USER_ID = "fb_sig_user";
private const string QUERY_AUTH_TOKEN = "auth_token";
private const string FACEBOOK_LOGIN_URL = @"http://www.facebook.com/login.php?api_key=";
private const string FACEBOOK_ADD_URL = @"http://www.facebook.com/add.php?api_key=";
private const string FACEBOOK_CANVAS_PARAM = "&canvas";

#endregion

#region Private Members

private FacebookService _fbService;

private bool _useSession;

private bool _autoAdd;

private HttpRequest _request;

private HttpResponse _response;

private HttpSessionState _session;

#endregion

#region Constructors

private BasePageHelper(FacebookService fbService, bool useSession, bool autoAdd, HttpRequest request,
HttpResponse response, HttpSessionState session)
{
_fbService = fbService;
_useSession = useSession;
_autoAdd = autoAdd;
_request = request;
_response = response;
_session = session;
}

#endregion

#region Public Static Methods

public static void LoadFBMLPage(FacebookService fbService, bool autoAdd, HttpRequest request, HttpResponse response)
{
string sessionKey = null;
string userId = null;
string inCanvas = request[REQUEST_IN_CANVAS];

fbService.IsDesktopApplication = false;

if (string.IsNullOrEmpty(fbService.SessionKey) || string.IsNullOrEmpty(fbService.UserId))
{
sessionKey = request[REQUEST_SESSION_KEY];
userId = request[REQUEST_USER_ID];
}
else
{
sessionKey = fbService.SessionKey;
userId = fbService.UserId;
}

// When the user uses the facebook login page, the redirect back here will will have the auth_token in the query params
string authToken = request.QueryString[QUERY_AUTH_TOKEN];

// We have already established a session on behalf of this user
if (!String.IsNullOrEmpty(sessionKey))
{
fbService.SessionKey = sessionKey;
fbService.UserId = userId;
}
//// This will be executed when facebook login redirects to our page
else if (!String.IsNullOrEmpty(authToken))
{
fbService.CreateSession(authToken);
}
else
{
//response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0");
response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
response.End();
}

if (!fbService.IsAppAdded() && autoAdd)
{
if ((!string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1")) || (!string.IsNullOrEmpty(request.Url.ToString()) && request.Url.ToString().ToLower().Contains("facebook.com")))
{
response.Write("<fb:redirect url=\"" + FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
response.End();
}
else
{
response.Redirect(FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0");
}
}
}

public static void LoadIFramePage(FacebookService fbService, bool useSession, bool autoAdd, HttpRequest request, HttpResponse response, HttpSessionState session)
{
new BasePageHelper(fbService, useSession, autoAdd, request, response, session).LoadIFramePage();
}

#endregion

#region Private Methods

private void LoadIFramePage()
{
_fbService.IsDesktopApplication = false;

// When the user uses the facebook login page, the redirect back here
// will will have the auth_token in the query params
string authToken = _request.QueryString[QUERY_AUTH_TOKEN];
string sessionKey = null;
string userId = null;

if (string.IsNullOrEmpty(_fbService.SessionKey) || string.IsNullOrEmpty(_fbService.UserId))
{
sessionKey = _request[REQUEST_SESSION_KEY];
userId = _request[REQUEST_USER_ID];
}
else
{
sessionKey = _fbService.SessionKey;
userId = _fbService.UserId;
}

// We have already established a session on behalf of this user
EstablishSession(sessionKey, userId, authToken, true);
}

private void EstablishSession(string sessionKey, string userId, string authToken, bool retry)
{
try
{
object sessionKeyFromSession = _useSession ? _session[SESSION_SESSION_KEY] :
_request.Cookies[SESSION_SESSION_KEY];

if (String.IsNullOrEmpty(sessionKey) && sessionKeyFromSession != null)
{
sessionKey = _useSession ? sessionKeyFromSession.ToString() : ((HttpCookie)sessionKeyFromSession).Value;
userId = _useSession ? _session[SESSION_USER_ID].ToString() : _request.Cookies[SESSION_USER_ID].Value;
}

if (!String.IsNullOrEmpty(sessionKey))
{
_fbService.SessionKey = sessionKey;
_fbService.UserId = userId;
SetSessionInfo(sessionKey, userId);
}
//// This will be executed when facebook login redirects to our page
else if (!String.IsNullOrEmpty(authToken))
{
_fbService.CreateSession(authToken);
SetSessionInfo(_fbService.SessionKey, _fbService.UserId);
}
else
{
RedirectToLogin();
return;
}

TryAddApp();
}
catch (Facebook.Exceptions.FacebookTimeoutException)
{
if (retry)
{
ClearSessionInfo();
EstablishSession(null, null, authToken, false);
}
else
{
throw;
}
}
}

private void RedirectTopFrame(HttpResponse response, string url)
{
response.Write("<script type=\"text/javascript\">\n" +
"if (parent != self) \n" +
"top.location.href = \"" + url + @"&v=1.0" + "\";\n" +
"else self.location.href = \"" + url + @"&v=1.0" + "\";\n" +
"</script>");
response.End();
}

private void SetSessionInfo(string sessionKey, string userId)
{
if (_useSession)
{
_session[SESSION_SESSION_KEY] = sessionKey;
_session[SESSION_USER_ID] = userId;
}
else
{
_response.Cookies.Clear();
_response.Cookies.Add(new HttpCookie(SESSION_SESSION_KEY, sessionKey));
_response.Cookies.Add(new HttpCookie(SESSION_USER_ID, userId));
}
}

private void ClearSessionInfo()
{
if (_useSession)
{
_session[SESSION_SESSION_KEY] = null;
_session[SESSION_USER_ID] = null;
}
else
{
_response.Cookies.Clear();
}
}

private void TryAddApp()
{
if (!_fbService.IsAppAdded() && _autoAdd)
{
if (_fbService.SessionKey != null)
{
_response.Cookies.Clear();
RedirectTopFrame(_response, FACEBOOK_ADD_URL + _fbService.ApplicationKey);
}
else
{
RedirectToLogin();
}
}
}

private void RedirectToLogin()
{
RedirectTopFrame(_response, FACEBOOK_LOGIN_URL + _fbService.ApplicationKey + FACEBOOK_CANVAS_PARAM);
}

#endregion
}
}
Show details Hide details

Change log

r10 by nberardi on Apr 11, 2008   Diff
added facebook controls for MVC
Go to: 

Older revisions

All revisions of this file

File info

Size: 9253 bytes, 256 lines
Hosted by Google Code