PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/IEE/CKS.InternetExtranetEdition/FormsBasedAuthentication/Code/ConfigManagement/EditMembershipProviders.cs

#
C# | 251 lines | 233 code | 13 blank | 5 comment | 26 complexity | 653df11bf53891c10ceafda1f21b2454 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Web.Security;
  5. using System.Configuration;
  6. using Microsoft.SharePoint;
  7. using Microsoft.SharePoint.WebControls;
  8. using Microsoft.SharePoint.Administration;
  9. using System.Web.UI.WebControls;
  10. using System.Reflection;
  11. namespace CKS.FormsBasedAuthentication
  12. {
  13. public class EditMembershipProviders : LayoutsPageBase
  14. {
  15. protected InputFormTextBox txtProvidername;
  16. protected InputFormCheckBox cbDefaultProvider;
  17. protected InputFormCheckBox cbSQLProvider;
  18. protected InputFormSection ifsSQLProvider;
  19. protected InputFormTextBox txtProviderType;
  20. protected DropDownList ddlConnectionNames;
  21. protected InputFormCheckBox cbEnablePasswordRetrieval;
  22. protected InputFormCheckBox cbEnablePasswordReset;
  23. protected InputFormCheckBox cbRequireQuestionAnswer;
  24. protected InputFormTextBox txtApplicationName;
  25. protected InputFormCheckBox cbRequireUniqueEmail;
  26. protected InputFormRadioButton rbClear;
  27. protected InputFormRadioButton rbEncrypted;
  28. protected InputFormRadioButton rbHashed;
  29. protected InputFormTextBox txtMaxInvalidAttempts;
  30. protected InputFormTextBox txtMinRequiredPasswordLength;
  31. protected InputFormTextBox txtMinRequiredNonAlphaChars;
  32. protected InputFormTextBox txtPasswordAttemptsWindow;
  33. protected InputFormTextBox txtPasswordStrengthRegEx;
  34. protected DropDownList ddlProviders;
  35. protected Button btnDelete;
  36. protected void Page_Load(object sender, EventArgs e)
  37. {
  38. if (!IsPostBack)
  39. {
  40. if (Request.Params["Status"] != string.Empty && Request.Params["Status"] == "Success")
  41. {
  42. RegisterClientScriptBlock("Confirmation", "<script>alert('Your Changes Were Made Successfully');</script>");
  43. }
  44. FillProviderList();
  45. FillConnectionNames();
  46. if (Membership.Provider != null)
  47. {
  48. ddlProviders.SelectedValue = Membership.Provider.Name;
  49. LoadProviderData(Membership.Provider.Name);
  50. }
  51. //Force the page to setup correctly
  52. ddlProviders_SelectedIndexChanged(ddlProviders, EventArgs.Empty);
  53. }
  54. }
  55. protected void FillProviderList()
  56. {
  57. ddlProviders.Items.Clear();
  58. ddlProviders.Items.Add(new ListItem("--New--"));
  59. foreach (MembershipProvider mp in Membership.Providers)
  60. {
  61. ddlProviders.Items.Add(mp.Name);
  62. }
  63. }
  64. protected void FillConnectionNames()
  65. {
  66. ddlConnectionNames.Items.Clear();
  67. foreach (System.Configuration.ConnectionStringSettings settings in System.Configuration.ConfigurationManager.ConnectionStrings)
  68. {
  69. ddlConnectionNames.Items.Add(new ListItem(settings.Name));
  70. }
  71. }
  72. protected void LoadProviderData(string providerName)
  73. {
  74. string connName = string.Empty;
  75. string connString = string.Empty;
  76. MembershipProvider provider = null;
  77. try
  78. {
  79. foreach (MembershipProvider mp in Membership.Providers)
  80. {
  81. if (mp.Name == providerName)
  82. {
  83. provider = mp;
  84. break;
  85. }
  86. }
  87. txtProvidername.Text = provider.Name;
  88. txtProviderType.Text = provider.GetType().AssemblyQualifiedName;
  89. if (Membership.Provider.Name == provider.Name)
  90. {
  91. cbDefaultProvider.Checked = true;
  92. btnDelete.Enabled = false;
  93. }
  94. else
  95. {
  96. cbDefaultProvider.Checked = false;
  97. btnDelete.Enabled = true;
  98. }
  99. if (provider is SqlMembershipProvider)
  100. {
  101. ifsSQLProvider.Visible = true;
  102. txtProviderType.Enabled = false;
  103. //Use Reflection trickery to get connection string
  104. FieldInfo connStringFI = provider.GetType().GetField("_sqlConnectionString", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  105. if (connStringFI != null)
  106. {
  107. connString = (string)connStringFI.GetValue(provider);
  108. //Now find the connection string from the config file if it exists
  109. foreach (System.Configuration.ConnectionStringSettings settings in System.Configuration.ConfigurationManager.ConnectionStrings)
  110. {
  111. if (settings.ConnectionString.Equals(connString)) //Found it
  112. ddlConnectionNames.SelectedValue = settings.Name;
  113. }
  114. }
  115. }
  116. else
  117. {
  118. txtProviderType.Enabled = true;
  119. ifsSQLProvider.Visible = false;
  120. }
  121. cbEnablePasswordRetrieval.Checked = provider.EnablePasswordRetrieval;
  122. cbEnablePasswordReset.Checked = provider.EnablePasswordReset;
  123. txtApplicationName.Text = provider.ApplicationName;
  124. cbRequireUniqueEmail.Checked = provider.RequiresUniqueEmail;
  125. if (provider.PasswordFormat == MembershipPasswordFormat.Clear)
  126. rbClear.Checked = true;
  127. else
  128. if (provider.PasswordFormat == MembershipPasswordFormat.Encrypted)
  129. rbEncrypted.Checked = true;
  130. else
  131. rbHashed.Checked = true;
  132. txtMaxInvalidAttempts.Text = provider.MaxInvalidPasswordAttempts.ToString();
  133. txtMinRequiredPasswordLength.Text = provider.MinRequiredPasswordLength.ToString();
  134. txtMinRequiredNonAlphaChars.Text = provider.MinRequiredNonAlphanumericCharacters.ToString();
  135. txtPasswordAttemptsWindow.Text = provider.PasswordAttemptWindow.ToString();
  136. txtPasswordStrengthRegEx.Text = provider.PasswordStrengthRegularExpression;
  137. }
  138. catch (Exception ex)
  139. {
  140. Utils.LogError(ex);
  141. }
  142. }
  143. protected void ddlProviders_SelectedIndexChanged(object sender, EventArgs e)
  144. {
  145. if (ddlProviders.SelectedValue == "--New--")
  146. SetupNewProvider();
  147. else
  148. LoadProviderData(ddlProviders.SelectedValue);
  149. }
  150. private void SetupNewProvider()
  151. {
  152. txtProvidername.Text = "MyMembershipProvider";
  153. cbDefaultProvider.Checked = false;
  154. txtProviderType.Text = typeof(SqlMembershipProvider).AssemblyQualifiedName;
  155. ddlConnectionNames.SelectedIndex = 0;
  156. cbEnablePasswordRetrieval.Checked = false;
  157. cbEnablePasswordReset.Checked = true;
  158. cbRequireQuestionAnswer.Checked = true;
  159. txtApplicationName.Text = "SharePoint";
  160. cbRequireUniqueEmail.Checked = true;
  161. rbHashed.Checked = true;
  162. txtMaxInvalidAttempts.Text = "5";
  163. txtMinRequiredPasswordLength.Text = "7";
  164. txtMinRequiredNonAlphaChars.Text = "1";
  165. txtPasswordAttemptsWindow.Text = "10";
  166. txtPasswordStrengthRegEx.Text = string.Empty;
  167. txtProvidername.Enabled = true;
  168. }
  169. protected void OnSubmit(object sender, EventArgs e)
  170. {
  171. SPSecurity.RunWithElevatedPrivileges(delegate()
  172. {
  173. System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  174. System.Web.Configuration.MembershipSection mSection = (System.Web.Configuration.MembershipSection)config.GetSection("system.web/membership");
  175. if (ddlProviders.SelectedValue == "--New--")
  176. {
  177. //Create the Provider using the settings from the page
  178. ProviderSettings pSettings = new System.Configuration.ProviderSettings(txtProvidername.Text, txtProviderType.Text);
  179. SetupMembershipProvider(config, ref pSettings);
  180. mSection.Providers.Add(pSettings);
  181. }
  182. else
  183. {
  184. ProviderSettings pSettings = mSection.Providers[ddlProviders.SelectedValue];
  185. SetupMembershipProvider(config, ref pSettings);
  186. }
  187. //Save Changes and Reset App
  188. config.Save(ConfigurationSaveMode.Minimal, false);
  189. Response.Redirect(Request.Url.ToString() + "?Status=Success");
  190. });
  191. }
  192. protected void cbSQLProvider_CheckChanged(object sender, EventArgs e)
  193. {
  194. if (cbSQLProvider.Checked)
  195. {
  196. ifsSQLProvider.Visible = true;
  197. txtProviderType.Enabled = false;
  198. }
  199. else
  200. {
  201. ifsSQLProvider.Visible = false;
  202. txtProviderType.Enabled = true;
  203. }
  204. }
  205. private void SetupMembershipProvider(System.Configuration.Configuration config, ref System.Configuration.ProviderSettings pSettings)
  206. {
  207. pSettings.Parameters["connectionStringName"] = ddlConnectionNames.SelectedValue;
  208. pSettings.Parameters["enablePasswordRetrieval"] = cbEnablePasswordRetrieval.Checked.ToString();
  209. pSettings.Parameters["enablePasswordReset"] = cbEnablePasswordReset.Checked.ToString();
  210. pSettings.Parameters["requiresQuestionAndAnswer"] = cbRequireQuestionAnswer.Checked.ToString();
  211. pSettings.Parameters["applicationName"] = txtApplicationName.Text;
  212. pSettings.Parameters["requiresUniqueEmail"] = cbRequireUniqueEmail.Checked.ToString();
  213. if (rbClear.Checked)
  214. pSettings.Parameters["passwordFormat"] = "Clear";
  215. else
  216. if (rbEncrypted.Checked)
  217. pSettings.Parameters["passwordFormat"] = "Encrypted";
  218. else
  219. pSettings.Parameters["passwordFormat"] = "Hashed";
  220. pSettings.Parameters["maxInvalidPasswordAttempts"] = txtMaxInvalidAttempts.Text;
  221. pSettings.Parameters["minRequiredPasswordLength"] = txtMinRequiredPasswordLength.Text;
  222. pSettings.Parameters["minRequiredNonalphanumericCharacters"] = txtMinRequiredNonAlphaChars.Text;
  223. pSettings.Parameters["passwordAttemptWindow"] = txtPasswordAttemptsWindow.Text;
  224. pSettings.Parameters["passwordStrengthRegularExpression"] = txtPasswordStrengthRegEx.Text;
  225. }
  226. protected void DeleteProvider(object sender, EventArgs e)
  227. {
  228. SPSecurity.RunWithElevatedPrivileges(delegate()
  229. {
  230. System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  231. System.Web.Configuration.MembershipSection mSection = (System.Web.Configuration.MembershipSection)config.GetSection("system.web/membership");
  232. mSection.Providers.Remove(txtProvidername.Text);
  233. config.Save(ConfigurationSaveMode.Minimal, false);
  234. Response.Redirect(Request.Url.AbsoluteUri.ToString() + "?Status=Success");
  235. });
  236. }
  237. }
  238. }