PageRenderTime 30ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/main-techdays/src/Veracruz.Facebook/Web/Security/FacebookMembershipProvider.cs

#
C# | 244 lines | 214 code | 20 blank | 10 comment | 20 complexity | 662a6ea4db3c8fc7d0e0ad6dc07ef023 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using Microsoft.IdentityModel.Claims;
  13. using Veracruz.IdentityModel;
  14. using Microsoft.IdentityModel.Web;
  15. using Veracruz.Facebook.IdentityModel;
  16. using System.Globalization;
  17. using System.Threading;
  18. namespace Veracruz.Facebook.Web.Security
  19. {
  20. public class FacebookMembershipProvider : MembershipProvider
  21. {
  22. #region fields
  23. private string m_applicationName;
  24. #endregion
  25. #region props
  26. public override string ApplicationName
  27. {
  28. get { return m_applicationName; }
  29. set { m_applicationName = value; }
  30. }
  31. public override bool EnablePasswordReset
  32. {
  33. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  34. }
  35. public override bool EnablePasswordRetrieval
  36. {
  37. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  38. }
  39. public override int MaxInvalidPasswordAttempts
  40. {
  41. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  42. }
  43. public override int MinRequiredNonAlphanumericCharacters
  44. {
  45. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  46. }
  47. public override int MinRequiredPasswordLength
  48. {
  49. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  50. }
  51. public override int PasswordAttemptWindow
  52. {
  53. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  54. }
  55. public override MembershipPasswordFormat PasswordFormat
  56. {
  57. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  58. }
  59. public override string PasswordStrengthRegularExpression
  60. {
  61. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  62. }
  63. public override bool RequiresQuestionAndAnswer
  64. {
  65. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  66. }
  67. public override bool RequiresUniqueEmail
  68. {
  69. get { throw new NotImplementedException(); } // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  70. }
  71. #endregion
  72. #region not implemented methods
  73. private Session GetSession()
  74. {
  75. IClaimsIdentity l_identity = Thread.CurrentPrincipal.Identity as IClaimsIdentity;
  76. if (l_identity == null)
  77. {
  78. throw new NotSupportedException();
  79. }
  80. Session l_session = null;
  81. if (String.Compare(l_identity.AuthenticationType, "Facebook", true, CultureInfo.InvariantCulture) == 0)
  82. {
  83. l_session = l_identity.GetSession();
  84. }
  85. else
  86. {
  87. ClaimsPrincipal __facebookPrincipal = HttpContext.Current.Session["CachedClaims"] as ClaimsPrincipal;
  88. if (__facebookPrincipal != null)
  89. {
  90. l_session = ((IClaimsIdentity)__facebookPrincipal.Identity).GetSession();
  91. }
  92. else
  93. {
  94. HttpContext.Current.Response.Redirect("~/_admin/facebookauth.aspx");
  95. }
  96. }
  97. if (l_session == null)
  98. {
  99. throw new NotSupportedException();
  100. }
  101. return l_session;
  102. }
  103. public override bool ChangePassword(string username, string oldPassword, string newPassword)
  104. {
  105. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  106. }
  107. public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
  108. {
  109. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  110. }
  111. public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
  112. {
  113. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  114. }
  115. public override bool DeleteUser(string username, bool deleteAllRelatedData)
  116. {
  117. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  118. }
  119. public override string GetPassword(string username, string answer)
  120. {
  121. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  122. }
  123. public override string ResetPassword(string username, string answer)
  124. {
  125. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  126. }
  127. public override bool UnlockUser(string userName)
  128. {
  129. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  130. }
  131. public override void UpdateUser(MembershipUser user)
  132. {
  133. throw new NotImplementedException(); // HACK : This membership provider is only a facade of the Facebook authentication mechanim.
  134. }
  135. #endregion
  136. #region methods
  137. public override string GetUserNameByEmail(string email)
  138. {
  139. return email; // The username is equals to the email.
  140. }
  141. public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
  142. {
  143. UserInfo[] l_userInfos = FacebookHelper.GetAllUserInfos(GetSession(), pageIndex, pageSize);
  144. totalRecords = l_userInfos.Length;
  145. MembershipUserCollection l_membershipUserCollection = new MembershipUserCollection();
  146. for (int i = 0; i < l_userInfos.Length; i++)
  147. {
  148. l_membershipUserCollection.Add(l_userInfos[i].CreateMembershipUser());
  149. }
  150. return l_membershipUserCollection;
  151. }
  152. public override MembershipUser GetUser(string username, bool userIsOnline)
  153. {
  154. if (string.IsNullOrEmpty(username))
  155. {
  156. throw new ArgumentNullException("username");
  157. }
  158. UserInfo l_userInfo = FacebookHelper.GetUserInfo(GetSession(), username);
  159. if (l_userInfo == null)
  160. return null;
  161. else
  162. return l_userInfo.CreateMembershipUser();
  163. }
  164. public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
  165. {
  166. if (!(providerUserKey is long))
  167. {
  168. throw new NotImplementedException();
  169. }
  170. UserInfo l_userInfo = FacebookHelper.GetUserInfo(GetSession(), (long)providerUserKey);
  171. return l_userInfo.CreateMembershipUser();
  172. }
  173. public override bool ValidateUser(string username, string password)
  174. {
  175. //if (!string.IsNullOrEmpty(password))
  176. //{
  177. // throw new ArgumentException("", "password");
  178. //}
  179. //IClaimsIdentity l_identity = (IClaimsIdentity)ClaimsPrincipal.Current.Identity;
  180. //string l_email = l_identity.GetClaimValue(System.IdentityModel.Claims.ClaimTypes.Email);
  181. //return (l_email == username);
  182. IClaimsIdentity __claimsIdentity = Thread.CurrentPrincipal.Identity as IClaimsIdentity;
  183. return __claimsIdentity != null && __claimsIdentity.IsAuthenticated;
  184. }
  185. public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
  186. {
  187. if (string.IsNullOrEmpty(usernameToMatch))
  188. {
  189. throw new ArgumentNullException("usernameToMatch");
  190. }
  191. UserInfo[] l_userInfos = FacebookHelper.FindUserInfosByFullName(GetSession(), usernameToMatch, pageIndex, pageSize);
  192. totalRecords = l_userInfos.Length;
  193. MembershipUserCollection l_membershipUserCollection = new MembershipUserCollection();
  194. for (int i = 0; i < l_userInfos.Length; i++)
  195. {
  196. l_membershipUserCollection.Add(l_userInfos[i].CreateMembershipUser());
  197. }
  198. return l_membershipUserCollection;
  199. //return this.FindUsersByEmail(usernameToMatch, pageIndex, pageSize, out totalRecords);
  200. }
  201. public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
  202. {
  203. if (string.IsNullOrEmpty(emailToMatch))
  204. {
  205. throw new ArgumentNullException("emailToMatch");
  206. }
  207. UserInfo[] l_userInfos = FacebookHelper.FindUserInfosByEmail(GetSession(), emailToMatch, pageIndex, pageSize);
  208. totalRecords = l_userInfos.Length;
  209. MembershipUserCollection l_membershipUserCollection = new MembershipUserCollection();
  210. for (int i = 0; i < l_userInfos.Length; i++)
  211. {
  212. l_membershipUserCollection.Add(l_userInfos[i].CreateMembershipUser());
  213. }
  214. return l_membershipUserCollection;
  215. }
  216. public override int GetNumberOfUsersOnline()
  217. {
  218. // HACK : Facebook provides currently a who is online feature only on friends of an user.
  219. // TODO : Develop a online mechanism on dedicated association facebook data store ?
  220. return FacebookHelper.GetNumberOfUsersOnline(GetSession());
  221. }
  222. #endregion
  223. }
  224. }