PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/facebook.Linq/Web/FacebookSession.cs

https://bitbucket.org/assaframan/facebooklinq
C# | 205 lines | 178 code | 27 blank | 0 comment | 20 complexity | d6f0b5482116d03c8b12a48e5e26f8b6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Configuration;
  6. using facebook.Linq;
  7. using System.Diagnostics;
  8. using facebook;
  9. namespace facebook.Web
  10. {
  11. public class FacebookContext
  12. {
  13. FacebookContext(HttpContext context)
  14. {
  15. Context = context;
  16. }
  17. HttpContext Context;
  18. public static FacebookContext Get(HttpContext context)
  19. {
  20. if (context == null)
  21. return null;
  22. var fc = context.Items["FacebookContext"] as FacebookContext;
  23. if (fc == null)
  24. {
  25. fc = new FacebookContext(context);
  26. context.Items["FacebookContext"] = fc;
  27. }
  28. return fc;
  29. }
  30. public static FacebookContext Current
  31. {
  32. get
  33. {
  34. return Get(HttpContext.Current);
  35. }
  36. }
  37. FacebookSession _Session;
  38. public FacebookSession Session
  39. {
  40. get
  41. {
  42. if (_Session == null)
  43. {
  44. var session = Context.Session;
  45. if (session == null)
  46. return null;
  47. _Session = session["FacebookSession"] as FacebookSession;
  48. if(_Session==null)
  49. {
  50. _Session = new FacebookSession();
  51. session["FacebookSession"] = _Session;
  52. }
  53. }
  54. return _Session;
  55. }
  56. }
  57. public bool TryAuthenticating(bool gotoLoginPageIfNeeded)
  58. {
  59. var x = Session.TryAuthenticating(Context.Request);
  60. if (!x)
  61. RedirectTopFrame(Facebook.FacebookLoginUrl);
  62. return x;
  63. }
  64. public bool TryAuthenticating()
  65. {
  66. return TryAuthenticating(false);
  67. }
  68. public static bool HasValidConfiguration
  69. {
  70. get
  71. {
  72. return Facebook.HasValidConfiguration;
  73. }
  74. }
  75. public static string FacebookLoginUrl
  76. {
  77. get
  78. {
  79. return Facebook.FacebookLoginUrl;
  80. }
  81. }
  82. internal void RedirectTopFrame(string url)
  83. {
  84. var response = Context.Response;
  85. response.ContentType = "text/html";
  86. response.Cache.SetNoStore();
  87. response.Cache.SetCacheability(HttpCacheability.NoCache);
  88. response.Cache.SetExpires(DateTime.Now.AddDays(-1));
  89. response.Write("<script type=\"text/javascript\">\n" +
  90. "if (parent != self) \n" +
  91. "top.location.href = \"" + url + "\";\n" +
  92. "else self.location.href = \"" + url + "\";\n" +
  93. "</script>");
  94. response.End();
  95. }
  96. }
  97. public class FacebookSession
  98. {
  99. protected internal FacebookSession()
  100. {
  101. }
  102. public static FacebookSession Current
  103. {
  104. get
  105. {
  106. return FacebookContext.Current.Session;
  107. }
  108. }
  109. public string SessionKey { get; set; }
  110. public long UserID { get; set; }
  111. public string AuthenticationToken { get; set; }
  112. public bool IsAuthenticated
  113. {
  114. get
  115. {
  116. return SessionKey.IsNotNullOrEmpty() && UserID>0;// && AuthenticationToken.IsNotNullOrEmpty();
  117. }
  118. }
  119. public bool TryAuthenticating(string fb_sig_session_key, string fb_sig_user, string auth_token)
  120. {
  121. if (IsAuthenticated)
  122. return true;
  123. SessionKey = fb_sig_session_key;
  124. if (fb_sig_user.IsNotNullOrEmpty())
  125. UserID = Int64.Parse(fb_sig_user);
  126. AuthenticationToken = auth_token;
  127. if (IsAuthenticated)
  128. {
  129. UpdateApi();
  130. return true;
  131. }
  132. else
  133. return false;
  134. }
  135. void UpdateApi()
  136. {
  137. if (_Api != null)
  138. {
  139. _Api.ApplicationKey = Facebook.APIKey;
  140. _Api.Secret = Facebook.Secret;
  141. _Api.AuthToken = AuthenticationToken;
  142. _Api.SessionKey = SessionKey;
  143. _Api.uid = UserID;
  144. }
  145. }
  146. public bool TryAuthenticating(HttpRequest request)
  147. {
  148. return TryAuthenticating(request["fb_sig_session_key"], request["fb_sig_user"], request["auth_token"]);
  149. }
  150. API _Api;
  151. public API Api
  152. {
  153. get
  154. {
  155. if (_Api == null)
  156. {
  157. _Api = new API();
  158. UpdateApi();
  159. }
  160. return _Api;
  161. }
  162. set
  163. {
  164. _Api = value;
  165. }
  166. }
  167. FacebookDataContext _Database;
  168. public FacebookDataContext Database
  169. {
  170. get
  171. {
  172. if (_Database == null)
  173. _Database = new FacebookDataContext(Api);
  174. return _Database;
  175. }
  176. }
  177. }
  178. }