1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.Security;
using AuthenticationComponent.BusinessFacade;
namespace AuthenticationComponent.WebUI
{
/// <summary>
/// Description résumée de AuthenticationControl.
/// </summary>
///
[DefaultProperty("Text"),
ToolboxData("<{0}:AuthenticationControl runat=server></{0}:AuthenticationControl>")]
public class AuthenticationControl : WebControl, INamingContainer
{
#region Member variables
//User
protected Label UserLabel = new Label();
protected TextBox UserTextBox = new TextBox();
protected RequiredFieldValidator UserRequiredFieldValidator = new RequiredFieldValidator();
protected RegularExpressionValidator UserRegularExpressionValidator = new RegularExpressionValidator();
//Password
protected Label PasswordLabel = new Label();
protected TextBox PasswordTextBox = new TextBox();
protected RequiredFieldValidator PasswordRequiredFieldValidator = new RequiredFieldValidator();
protected RegularExpressionValidator PasswordRegularExpressionValidator = new RegularExpressionValidator();
//Persitent Cookie
protected Label PersistLabel = new Label();
protected CheckBox Persist = new CheckBox();
protected ImageButton LogonButton = new ImageButton();
protected Panel MismatchPanel = new Panel();
protected Label ErrMsgLabel = new Label();
#endregion
#region Methods and Implementation
public AuthenticationControl()
{
EnsureChildControls();
}
/// <summary>
/// <para>CreateChildControls add all the control need to display in the current controls when is a composed control.</para>
/// <seealso cref="System.Web.UI.Control.Control" />
/// </summary>
/// <param name="evt">An EventArgs that controls the event data.</param>
protected override void CreateChildControls()
{
// Start to add all controls
Table table;
TableRow row;
TableCell cell;
table = new Table();
table.CellPadding = 5;
table.CellSpacing = 0;
Unit unit = Unit.Percentage(100);
table.Width = unit;
Controls.Add(table);
// User
row = new TableRow();
table.Rows.Add(row);
Controls.Add(UserLabel);
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Top;
UserLabel.Attributes.Add("key", "UserLabel");
cell.Controls.Add(UserLabel);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(UserTextBox);
cell.Controls.Add(new LiteralControl("*<BR>"));
UserTextBox.ID = "UserTextBox";
UserTextBox.CssClass = "TextBoxStyle";
UserRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
UserRequiredFieldValidator.ID = "UserRequired";
UserRequiredFieldValidator.ControlToValidate = UserTextBox.ID;
UserRequiredFieldValidator.Attributes.Add("key", "UserRequiredFieldValidator");
cell.Controls.Add(UserRequiredFieldValidator);
UserRegularExpressionValidator.Display = ValidatorDisplay.Dynamic;
UserRegularExpressionValidator.ID = "UserRegExp";
UserRegularExpressionValidator.ValidationExpression = "[^ ]{4,40}";
UserRegularExpressionValidator.ControlToValidate = UserTextBox.ID;
UserRegularExpressionValidator.Text = "User must be 4-40 nonblank characters.";
UserRegularExpressionValidator.Attributes.Add("key", "UserRegularExpressionValidator");
cell.Controls.Add(UserRegularExpressionValidator);
row.Cells.Add(cell);
//Password
row = new TableRow();
table.Rows.Add(row);
cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Top;
PasswordLabel.Attributes.Add("key", "PasswordLabel");
cell.Controls.Add(PasswordLabel);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(PasswordTextBox);
cell.Controls.Add(new LiteralControl("*<BR>"));
PasswordRequiredFieldValidator.Display = ValidatorDisplay.Dynamic;
PasswordTextBox.ID = "PasswordTextBox";
PasswordTextBox.TextMode = TextBoxMode.Password;
PasswordTextBox.CssClass = "TextBoxStyle";
PasswordRequiredFieldValidator.ID = "PasswordRequired";
PasswordRequiredFieldValidator.ControlToValidate = PasswordTextBox.ID;
PasswordRequiredFieldValidator.Attributes.Add("key", "PasswordRequiredFieldValidator");
cell.Controls.Add(PasswordRequiredFieldValidator);
PasswordRegularExpressionValidator.Display = ValidatorDisplay.Dynamic;
PasswordRegularExpressionValidator.ID = "PasswordRegExp";
PasswordRegularExpressionValidator.ValidationExpression = "[^ ]{4,40}";
PasswordRegularExpressionValidator.ControlToValidate = PasswordTextBox.ID;
PasswordRegularExpressionValidator.Text = "Password must be 4-40 nonblank characters.";
PasswordRegularExpressionValidator.Attributes.Add("key", "PasswordRegularExpressionValidator");
cell.Controls.Add(PasswordRegularExpressionValidator);
row.Cells.Add(cell);
//Persitent
row = new TableRow();
table.Rows.Add(row);
cell = new TableCell();
PersistLabel.Attributes.Add("key", "PersistLabel");
cell.Controls.Add(PersistLabel);
row.Cells.Add(cell);
cell = new TableCell();
cell.Controls.Add(Persist);
row.Cells.Add(cell);
//Logon Button
row = new TableRow();
table.Rows.Add(row);
cell = new TableCell();
cell.HorizontalAlign = HorizontalAlign.Right;
LogonButton.ImageUrl = "images/{0}/{1}/LogonButton.gif";
cell.Controls.Add(LogonButton);
row.Cells.Add(cell);
//Panel error
Controls.Add(MismatchPanel);
table = new Table();
table.CellPadding = 5;
table.CellSpacing = 0;
table.Width = unit;
MismatchPanel.Controls.Add(table);
row = new TableRow();
table.Rows.Add(row);
cell = new TableCell();
cell.Controls.Add(ErrMsgLabel);
row.Cells.Add(cell);
LogonButton.Click += new ImageClickEventHandler(OnLogin);
}
/// <summary>
/// Validates a logon attempt saves off the customer account information.
/// <param name="sender">The source of the event.</param>
/// <param name="e">An EventArgs that contains the event data.</param>
/// </summary>
private void OnLogin(object sender, ImageClickEventArgs e)
{
MismatchPanel.Visible = false;
//Validator controls make sure Email and Password exist
if (!Page.IsValid)
{
return;
}
//
// Check the User and Password combination
//
long party_id = 0;
try
{
party_id = (new UserLoginSystem()).GetPartyByUser(UserTextBox.Text, PasswordTextBox.Text);
}
catch(ArgumentException aex)
{
ErrMsgLabel.Text = "ArgumentException : " + aex.ToString();
MismatchPanel.Visible = true;
}
catch(Exception ex)
{
throw new Exception("Error : ",ex);
}
finally
{
if (party_id != 0) //were they valid?
{
FormsAuthentication.RedirectFromLoginPage(party_id.ToString(), Persist.Checked);
}
else
{
ErrMsgLabel.Attributes.Add("key", "UserNotRegistered");
MismatchPanel.Visible = true;
}
}
}
#endregion
}
} |
Partager