using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; namespace CustomPasswordField { public class PasswordField : SPField { // Fields private const string PROP_MINCHAR_IDX = "MinChars"; private const string PROP_STRONG_IDX = "StrongOnly"; private const string VAL_ERR_MSG_STRONG = "Password must have 3 of the 4 following: (1) at least 1 upper case character (2) at least 1 lower case character (3) at least 1 numerical character (4) at least 1 special character. Minimum Characters: {0}"; private const string VAL_ERR_MSG_WEAK = "Password must have at least {0} characters."; private const string VAL_MASK = "******"; private const int VAL_MAX_CHAR = 15; // Methods public PasswordField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { } public PasswordField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { } private string BuildValidationExpression() { if (this.StrongOnly) { return string.Concat(new object[] { "(?=^.{", this.MinChars, ",", 15, @"}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*" }); } return string.Concat(new object[] { @"^\w{", this.MinChars, ",", 15, "}$" }); } public override object GetFieldValue(string value) { if (this.UserHasEditPermission(SPContext.Current.Web.CurrentUser)) { return base.GetFieldValue(value); } return "******"; } public override string GetValidatedString(object value) { string input = value.ToString(); if (input.Length.Equals(0) && !base.Required) { return string.Empty; } if ((input.Length >= this.MinChars) || (input.Length <= 15)) { Regex regex = new Regex(this.BuildValidationExpression()); if (regex.IsMatch(input)) { return input; } } throw new SPFieldValidationException(this.GetValidationErrorString()); } public string GetValidationErrorString() { string format = this.StrongOnly ? "Password must have 3 of the 4 following: (1) at least 1 upper case character (2) at least 1 lower case character (3) at least 1 numerical character (4) at least 1 special character. Minimum Characters: {0}" : "Password must have at least {0} characters."; return string.Format(format, this.MinChars); } public bool UserHasEditPermission(SPUser user) { if (SPContext.Current.ListItem.ID.Equals(0)) { return SPContext.Current.List.DoesUserHavePermissions(SPBasePermissions.EditListItems | SPBasePermissions.AddListItems); } return SPContext.Current.ListItem.DoesUserHavePermissions(SPBasePermissions.EditListItems); } // Properties public override BaseFieldControl FieldRenderingControl { get { BaseFieldControl control = new PasswordFieldControl(); control.FieldName = base.InternalName; return control; } } public int MinChars { get { return Convert.ToInt32(base.GetCustomProperty("MinChars")); } set { base.SetCustomProperty("MinChars", value); } } public bool StrongOnly { get { return Convert.ToBoolean(base.GetCustomProperty("StrongOnly")); } set { base.SetCustomProperty("StrongOnly", value); } } } public class PasswordFieldControl : BaseFieldControl { // Fields private const string CTRL_BTN_ID = "GenerateButton"; private const string CTRL_LBL_ID = "DisplayLabel"; private const string CTRL_TXT_ID = "PasswordTextBox"; protected Label DisplayLabel; protected Button GenerateButton; protected TextBox PasswordTextBox; private const string PROP_DIS_TMPL = "SPFieldPasswordDisplayCtrl"; private const string PROP_EDT_TMPL = "SPFieldPasswordEditCtrl"; private const int VAL_ASC_MAX = 0x7d; private const int VAL_ASC_MIN = 0x21; private const int VAL_MAX_TRY = 20; // Methods protected override void CreateChildControls() { base.CreateChildControls(); PasswordField field = base.Field as PasswordField; if (field != null) { if (base.ControlMode == SPControlMode.Display) { this.DisplayLabel = this.TemplateContainer.FindControl("DisplayLabel") as Label; if (this.DisplayLabel != null) { this.DisplayLabel.Text = this.ItemFieldValue as string; } } else { this.PasswordTextBox = this.TemplateContainer.FindControl("PasswordTextBox") as TextBox; this.GenerateButton = this.TemplateContainer.FindControl("GenerateButton") as Button; if (field.UserHasEditPermission(SPContext.Current.Web.CurrentUser)) { this.PasswordTextBox.Enabled = true; this.GenerateButton.Enabled = true; this.GenerateButton.Click += new EventHandler(this.GenerateNewPassword); } else { this.PasswordTextBox.Enabled = false; this.GenerateButton.Enabled = false; } } } } private void GenerateNewPassword(object sender, EventArgs e) { Random random = new Random(DateTime.Now.Millisecond); StringBuilder builder = null; PasswordField field = base.Field as PasswordField; if ((field != null) && (this.PasswordTextBox != null)) { bool flag = false; int num = 0; do { builder = new StringBuilder(); for (int i = 0; i < field.MinChars; i++) { builder.Append((char) random.Next(0x21, 0x7d)); } try { field.GetValidatedString(builder.ToString()); flag = true; } catch (SPFieldValidationException) { flag = false; num++; } } while (!flag && (num < 20)); this.Value = builder.ToString(); } } // Properties protected override string DefaultTemplateName { get { return "SPFieldPasswordEditCtrl"; } } public override string DisplayTemplateName { get { return "SPFieldPasswordDisplayCtrl"; } } public override object Value { get { this.EnsureChildControls(); if (base.ControlMode == SPControlMode.Display) { if (this.DisplayLabel != null) { return this.DisplayLabel.Text; } } else if (this.PasswordTextBox != null) { return this.PasswordTextBox.Text; } return null; } set { this.EnsureChildControls(); if (base.ControlMode == SPControlMode.Display) { if (this.DisplayLabel != null) { this.DisplayLabel.Text = value.ToString(); } } else if (this.PasswordTextBox != null) { this.PasswordTextBox.Text = value.ToString(); } } } } }