PageRenderTime 85ms CodeModel.GetById 49ms RepoModel.GetById 4ms app.codeStats 0ms

/samples/OpenIdProviderWebForms/Code/ReadOnlyXmlMembershipProvider.cs

https://github.com/marcusmacinnes/dotnetopenid
C# | 270 lines | 186 code | 53 blank | 31 comment | 16 complexity | eb89afd90db55def85d5d13198573596 MD5 | raw file
  1. namespace OpenIdProviderWebForms.Code {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Configuration.Provider;
  6. using System.Security.Permissions;
  7. using System.Web;
  8. using System.Web.Hosting;
  9. using System.Web.Security;
  10. using System.Xml;
  11. public class ReadOnlyXmlMembershipProvider : MembershipProvider {
  12. private Dictionary<string, MembershipUser> users;
  13. private string xmlFileName;
  14. // MembershipProvider Properties
  15. public override string ApplicationName {
  16. get { throw new NotSupportedException(); }
  17. set { throw new NotSupportedException(); }
  18. }
  19. public override bool EnablePasswordRetrieval {
  20. get { return false; }
  21. }
  22. public override bool EnablePasswordReset {
  23. get { return false; }
  24. }
  25. public override int MaxInvalidPasswordAttempts {
  26. get { throw new NotSupportedException(); }
  27. }
  28. public override int MinRequiredNonAlphanumericCharacters {
  29. get { throw new NotSupportedException(); }
  30. }
  31. public override int MinRequiredPasswordLength {
  32. get { throw new NotSupportedException(); }
  33. }
  34. public override int PasswordAttemptWindow {
  35. get { throw new NotSupportedException(); }
  36. }
  37. public override MembershipPasswordFormat PasswordFormat {
  38. get { throw new NotSupportedException(); }
  39. }
  40. public override string PasswordStrengthRegularExpression {
  41. get { throw new NotSupportedException(); }
  42. }
  43. public override bool RequiresQuestionAndAnswer {
  44. get { throw new NotSupportedException(); }
  45. }
  46. public override bool RequiresUniqueEmail {
  47. get { throw new NotSupportedException(); }
  48. }
  49. // MembershipProvider Methods
  50. public override void Initialize(string name, NameValueCollection config) {
  51. // Verify that config isn't null
  52. if (config == null) {
  53. throw new ArgumentNullException("config");
  54. }
  55. // Assign the provider a default name if it doesn't have one
  56. if (string.IsNullOrEmpty(name)) {
  57. name = "ReadOnlyXmlMembershipProvider";
  58. }
  59. // Add a default "description" attribute to config if the
  60. // attribute doesn't exist or is empty
  61. if (string.IsNullOrEmpty(config["description"])) {
  62. config.Remove("description");
  63. config.Add("description", "Read-only XML membership provider");
  64. }
  65. // Call the base class's Initialize method
  66. base.Initialize(name, config);
  67. // Initialize _XmlFileName and make sure the path
  68. // is app-relative
  69. string path = config["xmlFileName"];
  70. if (string.IsNullOrEmpty(path)) {
  71. path = "~/App_Data/Users.xml";
  72. }
  73. if (!VirtualPathUtility.IsAppRelative(path)) {
  74. throw new ArgumentException("xmlFileName must be app-relative");
  75. }
  76. string fullyQualifiedPath = VirtualPathUtility.Combine(
  77. VirtualPathUtility.AppendTrailingSlash(HttpRuntime.AppDomainAppVirtualPath),
  78. path);
  79. this.xmlFileName = HostingEnvironment.MapPath(fullyQualifiedPath);
  80. config.Remove("xmlFileName");
  81. // Make sure we have permission to read the XML data source and
  82. // throw an exception if we don't
  83. FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, this.xmlFileName);
  84. permission.Demand();
  85. // Throw an exception if unrecognized attributes remain
  86. if (config.Count > 0) {
  87. string attr = config.GetKey(0);
  88. if (!string.IsNullOrEmpty(attr)) {
  89. throw new ProviderException("Unrecognized attribute: " + attr);
  90. }
  91. }
  92. }
  93. public override bool ValidateUser(string username, string password) {
  94. // Validate input parameters
  95. if (string.IsNullOrEmpty(username) ||
  96. string.IsNullOrEmpty(password)) {
  97. return false;
  98. }
  99. try {
  100. // Make sure the data source has been loaded
  101. this.ReadMembershipDataStore();
  102. // Validate the user name and password
  103. MembershipUser user;
  104. if (this.users.TryGetValue(username, out user)) {
  105. if (user.Comment == password) { // Case-sensitive
  106. // NOTE: A read/write membership provider
  107. // would update the user's LastLoginDate here.
  108. // A fully featured provider would also fire
  109. // an AuditMembershipAuthenticationSuccess
  110. // Web event
  111. return true;
  112. }
  113. }
  114. // NOTE: A fully featured membership provider would
  115. // fire an AuditMembershipAuthenticationFailure
  116. // Web event here
  117. return false;
  118. } catch (Exception) {
  119. return false;
  120. }
  121. }
  122. public override MembershipUser GetUser(string username, bool userIsOnline) {
  123. // Note: This implementation ignores userIsOnline
  124. // Validate input parameters
  125. if (string.IsNullOrEmpty(username)) {
  126. return null;
  127. }
  128. // Make sure the data source has been loaded
  129. this.ReadMembershipDataStore();
  130. // Retrieve the user from the data source
  131. MembershipUser user;
  132. if (this.users.TryGetValue(username, out user)) {
  133. return user;
  134. }
  135. return null;
  136. }
  137. public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
  138. // Note: This implementation ignores pageIndex and pageSize,
  139. // and it doesn't sort the MembershipUser objects returned
  140. // Make sure the data source has been loaded
  141. this.ReadMembershipDataStore();
  142. MembershipUserCollection users = new MembershipUserCollection();
  143. foreach (KeyValuePair<string, MembershipUser> pair in this.users) {
  144. users.Add(pair.Value);
  145. }
  146. totalRecords = users.Count;
  147. return users;
  148. }
  149. public override int GetNumberOfUsersOnline() {
  150. throw new NotSupportedException();
  151. }
  152. public override bool ChangePassword(string username, string oldPassword, string newPassword) {
  153. throw new NotSupportedException();
  154. }
  155. public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
  156. throw new NotSupportedException();
  157. }
  158. public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
  159. throw new NotSupportedException();
  160. }
  161. public override bool DeleteUser(string username, bool deleteAllRelatedData) {
  162. throw new NotSupportedException();
  163. }
  164. public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
  165. throw new NotSupportedException();
  166. }
  167. public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
  168. throw new NotSupportedException();
  169. }
  170. public override string GetPassword(string username, string answer) {
  171. throw new NotSupportedException();
  172. }
  173. public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
  174. throw new NotSupportedException();
  175. }
  176. public override string GetUserNameByEmail(string email) {
  177. throw new NotSupportedException();
  178. }
  179. public override string ResetPassword(string username, string answer) {
  180. throw new NotSupportedException();
  181. }
  182. public override bool UnlockUser(string userName) {
  183. throw new NotSupportedException();
  184. }
  185. public override void UpdateUser(MembershipUser user) {
  186. throw new NotSupportedException();
  187. }
  188. // Helper method
  189. private void ReadMembershipDataStore() {
  190. lock (this) {
  191. if (this.users == null) {
  192. this.users = new Dictionary<string, MembershipUser>(16, StringComparer.InvariantCultureIgnoreCase);
  193. XmlDocument doc = new XmlDocument();
  194. doc.Load(this.xmlFileName);
  195. XmlNodeList nodes = doc.GetElementsByTagName("User");
  196. foreach (XmlNode node in nodes) {
  197. MembershipUser user = new MembershipUser(
  198. Name, // Provider name
  199. node["UserName"].InnerText, // Username
  200. null, // providerUserKey
  201. null, // Email
  202. string.Empty, // passwordQuestion
  203. node["Password"].InnerText, // Comment
  204. true, // isApproved
  205. false, // isLockedOut
  206. DateTime.Now, // creationDate
  207. DateTime.Now, // lastLoginDate
  208. DateTime.Now, // lastActivityDate
  209. DateTime.Now, // lastPasswordChangedDate
  210. new DateTime(1980, 1, 1)); // lastLockoutDate
  211. this.users.Add(user.UserName, user);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }