PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/CMSModules/Membership/FormControls/Passwords/PasswordStrength.ascx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 467 lines | 327 code | 73 blank | 67 comment | 14 complexity | 367638eaf5b9f93961c2c328790259f8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Text.RegularExpressions;
  8. using CMS.FormEngine;
  9. using CMS.FormControls;
  10. using CMS.GlobalHelper;
  11. using CMS.SettingsProvider;
  12. using CMS.CMSHelper;
  13. using System.Text;
  14. public partial class CMSModules_Membership_FormControls_Passwords_PasswordStrength : FormEngineUserControl
  15. {
  16. #region "Variables"
  17. string mSiteName = null;
  18. int mPreferedLength = 12;
  19. int mPreferedNonAlphaNumChars = 2;
  20. string mClassPrefix = "PasswordStrength";
  21. bool mAllowEmpty = false;
  22. bool mShowValidationOnNewLine = false;
  23. string mValidationGroup = string.Empty;
  24. string mTextBoxClass = "TextBoxField";
  25. bool mUseStylesForStrenghtIndicator = true;
  26. private bool mShowStrengthIndicator = true;
  27. #endregion
  28. #region "Private properties"
  29. /// <summary>
  30. /// Returns current site name.
  31. /// </summary>
  32. private string SiteName
  33. {
  34. get
  35. {
  36. if (mSiteName == null)
  37. {
  38. mSiteName = CMSContext.CurrentSiteName;
  39. }
  40. return mSiteName;
  41. }
  42. }
  43. /// <summary>
  44. /// Returns whether password policy is used.
  45. /// </summary>
  46. private bool UsePasswordPolicy
  47. {
  48. get
  49. {
  50. return SettingsKeyProvider.GetBoolValue(SiteName + ".CMSUsePasswordPolicy");
  51. }
  52. }
  53. /// <summary>
  54. /// Returns password minimal length.
  55. /// </summary>
  56. private int MinLength
  57. {
  58. get
  59. {
  60. return SettingsKeyProvider.GetIntValue(SiteName + ".CMSPolicyMinimalLength");
  61. }
  62. }
  63. /// <summary>
  64. /// Returns number of non alpha numeric characters
  65. /// </summary>
  66. private int MinNonAlphaNumChars
  67. {
  68. get
  69. {
  70. return SettingsKeyProvider.GetIntValue(SiteName + ".CMSPolicyNumberOfNonAlphaNumChars");
  71. }
  72. }
  73. /// <summary>
  74. /// Returns password gegular expression.
  75. /// </summary>
  76. private string RegularExpression
  77. {
  78. get
  79. {
  80. return SettingsKeyProvider.GetStringValue(SiteName + ".CMSPolicyRegularExpression");
  81. }
  82. }
  83. #endregion
  84. #region "Public properties"
  85. /// <summary>
  86. /// Gets or sets the value that indicates whether inline styles should be used for strenght indicator
  87. /// </summary>
  88. public bool UseStylesForStrenghtIndicator
  89. {
  90. get
  91. {
  92. return mUseStylesForStrenghtIndicator;
  93. }
  94. set
  95. {
  96. mUseStylesForStrenghtIndicator = value;
  97. }
  98. }
  99. /// <summary>
  100. /// Gets or sets value of from control.
  101. /// </summary>
  102. public override object Value
  103. {
  104. get
  105. {
  106. return txtPassword.Text;
  107. }
  108. set
  109. {
  110. txtPassword.Text = ValidationHelper.GetString(value, string.Empty);
  111. }
  112. }
  113. /// <summary>
  114. /// Gets or sets prefered length.
  115. /// </summary>
  116. public int PreferedLength
  117. {
  118. get
  119. {
  120. return mPreferedLength;
  121. }
  122. set
  123. {
  124. mPreferedLength = value;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets or sets prefered number of non alpha numeric characters.
  129. /// </summary>
  130. public int PreferedNonAlphaNumChars
  131. {
  132. get
  133. {
  134. return mPreferedNonAlphaNumChars;
  135. }
  136. set
  137. {
  138. mPreferedNonAlphaNumChars = value;
  139. }
  140. }
  141. /// <summary>
  142. /// Class prefix for labels.
  143. /// </summary>
  144. public string ClassPrefix
  145. {
  146. get
  147. {
  148. return mClassPrefix;
  149. }
  150. set
  151. {
  152. mClassPrefix = value;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets or sets value of from control in string type.
  157. /// </summary>
  158. public override string Text
  159. {
  160. get
  161. {
  162. return txtPassword.Text;
  163. }
  164. set
  165. {
  166. txtPassword.Text = value;
  167. }
  168. }
  169. /// <summary>
  170. /// Gets or sets whether password could be empty.
  171. /// </summary>
  172. public bool AllowEmpty
  173. {
  174. get
  175. {
  176. return mAllowEmpty;
  177. }
  178. set
  179. {
  180. mAllowEmpty = value;
  181. }
  182. }
  183. /// <summary>
  184. /// Gets or sets whether validation control is shown under the control.
  185. /// </summary>
  186. public bool ShowValidationOnNewLine
  187. {
  188. get
  189. {
  190. return mShowValidationOnNewLine;
  191. }
  192. set
  193. {
  194. mShowValidationOnNewLine = value;
  195. }
  196. }
  197. /// <summary>
  198. /// Gets or sets validation group.
  199. /// </summary>
  200. public string ValidationGroup
  201. {
  202. get
  203. {
  204. return mValidationGroup;
  205. }
  206. set
  207. {
  208. mValidationGroup = value;
  209. }
  210. }
  211. /// <summary>
  212. /// Gets or sets class of textbox.
  213. /// </summary>
  214. public string TextBoxClass
  215. {
  216. get
  217. {
  218. return mTextBoxClass;
  219. }
  220. set
  221. {
  222. mTextBoxClass = value;
  223. }
  224. }
  225. /// <summary>
  226. /// Returns textbox attributes.
  227. /// </summary>
  228. public AttributeCollection TextBoxAttributes
  229. {
  230. get
  231. {
  232. return txtPassword.Attributes;
  233. }
  234. }
  235. /// <summary>
  236. /// Gets or sets maximal length of password.
  237. /// </summary>
  238. public int MaxLength
  239. {
  240. get
  241. {
  242. return txtPassword.MaxLength;
  243. }
  244. set
  245. {
  246. txtPassword.MaxLength = value;
  247. }
  248. }
  249. /// <summary>
  250. /// Gets or sets HTML that is displayed next to password input and indicates password as required field.
  251. /// </summary>
  252. public string RequiredFieldMark
  253. {
  254. get
  255. {
  256. return this.lblRequiredFieldMark.Text;
  257. }
  258. set
  259. {
  260. this.lblRequiredFieldMark.Text = value;
  261. }
  262. }
  263. /// <summary>
  264. /// Gets or sets whether strength indicator is shown.
  265. /// </summary>
  266. public bool ShowStrengthIndicator
  267. {
  268. get
  269. {
  270. return mShowStrengthIndicator;
  271. }
  272. set
  273. {
  274. mShowStrengthIndicator = value;
  275. }
  276. }
  277. #endregion
  278. #region "Page events"
  279. protected void Page_Load(object sender, EventArgs e)
  280. {
  281. // Set class
  282. txtPassword.CssClass = TextBoxClass;
  283. if (ShowStrengthIndicator)
  284. {
  285. string tooltipMessage = string.Empty;
  286. StringBuilder sb = new StringBuilder();
  287. if (UsePasswordPolicy)
  288. {
  289. sb.Append(GetString("passwordstrength.notacceptable"), ";", GetString("passwordstrength.weak"));
  290. tooltipMessage = string.Format(GetString("passwordstrength.hint"), MinLength, MinNonAlphaNumChars, PreferedLength, PreferedNonAlphaNumChars);
  291. }
  292. else
  293. {
  294. sb.Append(GetString("passwordstrength.weak"), ";", GetString("passwordstrength.weak"));
  295. tooltipMessage = string.Format(GetString("passwordstrength.recommend"), PreferedLength, PreferedNonAlphaNumChars);
  296. }
  297. // Register jQuery and registration of script which shows password strength
  298. ScriptHelper.RegisterJQuery(this.Page);
  299. ScriptHelper.RegisterScriptFile(this.Page, "~/CMSScripts/membership.js");
  300. sb.Append(";", GetString("passwordstrength.acceptable"), ";", GetString("passwordstrength.average"), ";", GetString("passwordstrength.strong"), ";", GetString("passwordstrength.excellent"));
  301. string regex = "''";
  302. if (!string.IsNullOrEmpty(RegularExpression))
  303. {
  304. regex = "/" + RegularExpression + "/";
  305. }
  306. // Javascript for calling js function on keyup of textbox
  307. string txtVar = "txtSearch_" + txtPassword.ClientID;
  308. string script =
  309. txtVar + " = $j('#" + txtPassword.ClientID + @"');
  310. if (" + txtVar + @" ) {
  311. " + txtVar + @".keyup(function(event){
  312. ShowStrength('" + txtPassword.ClientID + "', '" + MinLength + "', '" + PreferedLength + "', '" + MinNonAlphaNumChars + "', '"
  313. + PreferedNonAlphaNumChars + "', " + regex + ", '" + lblEvaluation.ClientID + "', '" + sb.ToString() + "', '" + ClassPrefix + "', '" + UsePasswordPolicy + "', '" + pnlPasswIndicator.ClientID + "', '" + UseStylesForStrenghtIndicator + @"');
  314. });
  315. }";
  316. ScriptHelper.RegisterStartupScript(this, typeof(string), "PasswordStrength_" + txtPassword.ClientID, ScriptHelper.GetScript(script));
  317. if (UseStylesForStrenghtIndicator)
  318. {
  319. pnlPasswStrengthIndicator.Style.Add("height", "5px");
  320. pnlPasswStrengthIndicator.Style.Add("background-color", "#dddddd");
  321. pnlPasswIndicator.Style.Add("height", "5px");
  322. }
  323. ScriptHelper.RegisterTooltip(this.Page);
  324. ScriptHelper.AppendTooltip(lblPasswStregth, tooltipMessage, "help");
  325. }
  326. else
  327. {
  328. pnlPasswStrengthIndicator.Visible = false;
  329. lblEvaluation.Visible = false;
  330. lblPasswStregth.Visible = false;
  331. }
  332. // Set up required field validator
  333. if (AllowEmpty)
  334. {
  335. rfvPassword.Enabled = false;
  336. }
  337. else
  338. {
  339. rfvPassword.Text = GetString("general.requirespassword");
  340. rfvPassword.ValidationGroup = ValidationGroup;
  341. if (ShowValidationOnNewLine)
  342. {
  343. rfvPassword.Text += "<br />";
  344. }
  345. }
  346. }
  347. protected override void OnPreRender(EventArgs e)
  348. {
  349. base.OnPreRender(e);
  350. this.lblRequiredFieldMark.Visible = !String.IsNullOrEmpty(this.lblRequiredFieldMark.Text);
  351. }
  352. #endregion
  353. #region "Methods"
  354. /// <summary>
  355. /// Returns whether
  356. /// </summary>
  357. public override bool IsValid()
  358. {
  359. if (UsePasswordPolicy)
  360. {
  361. string password = txtPassword.Text;
  362. this.ValidationError = GetString("passwordpolicy.notaccetable");
  363. // Check minimal length
  364. if (password.Length < MinLength)
  365. {
  366. return false;
  367. }
  368. // Check number of non alphanum characters
  369. int counter = 0;
  370. foreach (char c in password)
  371. {
  372. if (!char.IsLetterOrDigit(c))
  373. {
  374. counter++;
  375. }
  376. }
  377. if (counter < MinNonAlphaNumChars)
  378. {
  379. return false;
  380. }
  381. // Check regular expression
  382. if (!string.IsNullOrEmpty(RegularExpression))
  383. {
  384. Regex regex = new Regex(RegularExpression);
  385. if (!regex.IsMatch(password))
  386. {
  387. return false;
  388. }
  389. }
  390. }
  391. return true;
  392. }
  393. #endregion
  394. }