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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
//------------------------------------------------------------------------------
// <copyright file="LoginUtil.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Net.Mail;
using System.Security.Principal;
using System.Web.Security;
// Utility methods used by the Login controls
internal static class LoginUtil {
private const string _userNameReplacementKey = "<%\\s*UserName\\s*%>";
private const string _passwordReplacementKey = "<%\\s*Password\\s*%>";
private const string _templateDesignerRegion = "0";
// Will apply style to literals if provided and determines if it should be visible
internal static void ApplyStyleToLiteral(Literal literal, string text, Style style, bool setTableCellVisible) {
// setTableCellVisible is used when we DO NOT make the whole table cell invisible, because in some layouts
// it must exist for things to align correctly and its uncommon that this property will be empty anyway.
bool visible = false;
if (!String.IsNullOrEmpty(text)) {
literal.Text = text;
if (style != null) {
LoginUtil.SetTableCellStyle(literal, style);
}
visible = true;
}
if (setTableCellVisible) {
LoginUtil.SetTableCellVisible(literal, visible);
}
else {
literal.Visible = visible;
}
}
// These two functions are used by the LoginControls for the border table styles
internal static void CopyBorderStyles(WebControl control, Style style) {
if (style == null || style.IsEmpty) {
return;
}
control.BorderStyle = style.BorderStyle;
control.BorderColor = style.BorderColor;
control.BorderWidth = style.BorderWidth;
control.BackColor = style.BackColor;
control.CssClass = style.CssClass;
}
internal static void CopyStyleToInnerControl(WebControl control, Style style) {
if (style == null || style.IsEmpty) {
return;
}
control.ForeColor = style.ForeColor;
control.Font.CopyFrom(style.Font);
}
internal static Table CreateChildTable(bool convertingToTemplate) {
if (convertingToTemplate) {
return new Table();
}
return new ChildTable(2);
}
private static MailMessage CreateMailMessage(string email, string userName, string password,
MailDefinition mailDefinition, string defaultBody,
Control owner) {
ListDictionary replacements = new ListDictionary();
// Need to html encode the username and password if the body is HTML
if (mailDefinition.IsBodyHtml) {
userName = HttpUtility.HtmlEncode(userName);
password = HttpUtility.HtmlEncode(password);
}
replacements.Add(_userNameReplacementKey, userName);
replacements.Add(_passwordReplacementKey, password);
if (String.IsNullOrEmpty(mailDefinition.BodyFileName) && defaultBody != null) {
return mailDefinition.CreateMailMessage(email, replacements, defaultBody, owner);
}
else {
return mailDefinition.CreateMailMessage(email, replacements, owner);
}
}
internal static MembershipProvider GetProvider(string providerName) {
MembershipProvider provider;
if (String.IsNullOrEmpty(providerName)) {
provider = Membership.Provider;
}
else {
provider = Membership.Providers[providerName];
if (provider == null) {
throw new HttpException(SR.GetString(SR.WebControl_CantFindProvider));
}
}
return provider;
}
// Returns the IPrincipal of the currently logged in user. Returns null if no user
// is logged in, or if Page and HttpContext are not available.
internal static IPrincipal GetUser(Control c) {
IPrincipal user = null;
Page page = c.Page;
if (page != null) {
user = page.User;
}
else {
HttpContext context = HttpContext.Current;
if (context != null) {
user = context.User;
}
}
return user;
}
// Returns the username of the currently logged in user. Returns null or String.Empty
// if no user is logged in, or if Page and HttpContext are not available.
internal static string GetUserName(Control c) {
string userName = null;
IPrincipal user = GetUser(c);
if (user != null) {
IIdentity identity = user.Identity;
if (identity != null) {
userName = identity.Name;
}
}
return userName;
}
internal static void SendPasswordMail(string email, string userName, string password,
MailDefinition mailDefinition,
string defaultSubject, string defaultBody,
OnSendingMailDelegate onSendingMailDelegate,
OnSendMailErrorDelegate onSendMailErrorDelegate,
Control owner) {
// If the MailAddress ctor throws an exception, raise the error event but do not
// rethrow the exception. We do not rethrow the exception since the email address
// is user-entered data, and it should not cause an unhandled exception in the page.
// If any other part of creating or sending the MailMessage throws an exception,
// it is most likely a developer error so the exception should be rethrown.
// (VSWhidbey 490984)
try {
new MailAddress(email);
}
catch (Exception e) {
SendMailErrorEventArgs args = new SendMailErrorEventArgs(e);
// SendMailErrorEventArgs.Handled should be true, to indicate that the exception
// will not be rethrown. (VSWhidbey 529233)
args.Handled = true;
onSendMailErrorDelegate(args);
return;
}
try {
using (MailMessage message = CreateMailMessage(email, userName, password,
mailDefinition, defaultBody,
owner)) {
if (mailDefinition.SubjectInternal == null && defaultSubject != null) {
message.Subject = defaultSubject;
}
MailMessageEventArgs args = new MailMessageEventArgs(message);
onSendingMailDelegate(args);
if (args.Cancel) {
return;
}
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
}
} catch (Exception e) {
SendMailErrorEventArgs args = new SendMailErrorEventArgs(e);
onSendMailErrorDelegate(args);
// If the error wasn't handled, we rethrow
if (!args.Handled) {
throw;
}
}
}
// Sets the style of the table cell that contains the control.
internal static void SetTableCellStyle(Control control, Style style) {
Control parent = control.Parent;
if (parent != null) {
((TableCell) parent).ApplyStyle(style);
}
}
// Sets the visibility of the table cell that contains the control. The whole cell is made invisible
// to shrink rendered size and improve rendered appearance if cell padding or spacing is set.
internal static void SetTableCellVisible(Control control, bool visible) {
Control parent = control.Parent;
if (parent != null) {
parent.Visible = visible;
}
}
internal delegate void OnSendingMailDelegate(MailMessageEventArgs e);
internal delegate void OnSendMailErrorDelegate(SendMailErrorEventArgs e);
/// <devdoc>
/// TableRow that only renders if any of its cells are visible. Improves the appearance
/// of the control by removing empty rows. Use this class instead of changing the
/// visibility of the table rows, since that causes problems in the designer.
/// (VSWhidbey 81265)
/// </devdoc>
internal sealed class DisappearingTableRow : TableRow {
protected internal override void Render(HtmlTextWriter writer) {
bool rowVisible = false;
foreach (TableCell cell in Cells) {
if (cell.Visible) {
rowVisible = true;
break;
}
}
if (rowVisible) {
base.Render(writer);
}
}
}
/// <devdoc>
/// The base class for all containers used for individual views in the
/// Login and PasswordRecovery controls. Internal because used from PasswordRecovery.
/// </devdoc>
internal abstract class GenericContainer<ControlType> : WebControl where ControlType : WebControl,
IBorderPaddingControl,
IRenderOuterTableControl {
private bool _renderDesignerRegion = false;
private ControlType _owner;
private Table _layoutTable;
private Table _borderTable;
public GenericContainer(ControlType owner) {
_owner = owner;
}
internal int BorderPadding {
get {
return _owner.BorderPadding;
}
}
internal Table BorderTable {
get {
return _borderTable;
}
set {
_borderTable = value;
}
}
protected abstract bool ConvertingToTemplate { get; }
internal Table LayoutTable {
get {
return _layoutTable;
}
set {
_layoutTable = value;
}
}
internal ControlType Owner {
get {
return _owner;
}
}
internal bool RenderDesignerRegion {
get {
return DesignMode && _renderDesignerRegion;
}
set {
_renderDesignerRegion = value;
}
}
private bool RenderOuterTable {
get {
return _owner.RenderOuterTable;
}
}
// Returns true when using the default template, and false when using a custom template.
private bool UsingDefaultTemplate {
get {
return (BorderTable != null);
}
}
public sealed override void Focus() {
throw new NotSupportedException(SR.GetString(SR.NoFocusSupport, this.GetType().Name));
}
private Control FindControl<RequiredType>(string id, bool required, string errorResourceKey) {
Control control = FindControl(id);
if (control is RequiredType) {
return control;
}
else {
// Do not throw exception at design-time (VSWhidbey 429452)
if (required && !Owner.DesignMode) {
throw new HttpException(SR.GetString(errorResourceKey, Owner.ID, id));
}
else {
return null;
}
}
}
protected Control FindOptionalControl<RequiredType>(string id) {
return FindControl<RequiredType>(id, false, null);
}
protected Control FindRequiredControl<RequiredType>(string id, string errorResourceKey) {
return FindControl<RequiredType>(id, true, errorResourceKey);
}
protected internal virtual string ModifiedOuterTableStylePropertyName() {
// Verify that style properties are not set (not different than their defaults).
if (BorderPadding != 1) {
return "BorderPadding";
}
return ModifiedOuterTableBasicStylePropertyName(Owner);
}
/// <devdoc>
/// Renders the template contents. The default template is rendered directly since it is already a table.
/// A user template is rendered inside a table with one row and one cell.
/// User a single-cell table instead of a <div>, since the <div> always spans the full width of its
/// containing element, while a <table> sets width to contents.
/// </devdoc>
protected internal sealed override void Render(HtmlTextWriter writer) {
if (!RenderOuterTable) {
string propertyName = ModifiedOuterTableStylePropertyName();
if (!string.IsNullOrEmpty(propertyName)) {
throw new InvalidOperationException(SR.GetString(SR.IRenderOuterTableControl_CannotSetStyleWhenDisableRenderOuterTable,
propertyName, _owner.GetType().Name, _owner.ID));
}
}
if (UsingDefaultTemplate) {
// If we are converting to template, then do not apply the base attributes or ControlStyle
// to the BorderTable or LayoutTable. These will be rendered around the template contents
// at runtime.
if (!ConvertingToTemplate) {
BorderTable.CopyBaseAttributes(this);
if (ControlStyleCreated) {
// I assume we apply the BorderStyles and Font/ForeColor separately to make the rendering
// work in IE Quirks mode. If we only wanted to support Standards mode, we could probably
// apply all the styles to the BorderTable. I'm not changing this for Whidbey RTM
// since it is high-risk.
LoginUtil.CopyBorderStyles(BorderTable, ControlStyle);
LoginUtil.CopyStyleToInnerControl(LayoutTable, ControlStyle);
}
}
// Need to set height and width even when converting to template, to force the inner
// LayoutTable to fill the contents of the outer control.
LayoutTable.Height = Height;
LayoutTable.Width = Width;
if (RenderOuterTable) {
RenderContents(writer);
}
else {
// just render what would have been rendered in the outer table's single cell
ControlCollection controlsInCell = BorderTable.Rows[0].Cells[0].Controls;
RenderControls(writer, controlsInCell);
}
}
else {
RenderContentsInUnitTable(writer);
}
}
private void RenderContentsInUnitTable(HtmlTextWriter writer) {
// there are two situations in which we need an outer table:
// 1) it was explicitly asked for (RenderOuterTable property), or
// 2) a custom template is in use and the designer needs a container to host
// the editable inner region (RenderDesignerRegion property)
if (!RenderOuterTable && !RenderDesignerRegion) {
RenderControls(writer, Controls);
return;
}
LayoutTable table = new LayoutTable(1, 1, Page);
// Don't render out the child controls if we are using region editing, just output the region attribute
if (RenderDesignerRegion) {
table[0, 0].Attributes[HtmlTextWriter.DesignerRegionAttributeName] = _templateDesignerRegion;
}
else {
foreach (Control child in Controls) {
table[0, 0].Controls.Add(child);
}
}
// don't want to copy any style attributes to the outer table if we're only rendering it to satisfy
// the designer, otherwise the designer will show styles that won't be rendered on an actual page
if (RenderOuterTable) {
string parentID = Parent.ID;
if ((parentID != null) && (parentID.Length != 0)) {
table.ID = Parent.ClientID;
}
table.CopyBaseAttributes(this);
table.ApplyStyle(ControlStyle);
// This table should not add any cell padding or spacing around the template contents
table.CellPadding = 0;
table.CellSpacing = 0;
}
table.RenderControl(writer);
}
private static void RenderControls(HtmlTextWriter writer, ControlCollection controls) {
foreach (Control child in controls) {
child.RenderControl(writer);
}
}
// Throws an exception if a control with the specified id and type is found within
// the container. Does not throw exception at design-time.
protected void VerifyControlNotPresent<RequiredType>(string id, string errorResourceKey) {
Control control = FindOptionalControl<RequiredType>(id);
if (control != null && !Owner.DesignMode) {
throw new HttpException(SR.GetString(errorResourceKey, Owner.ID, id));
}
}
}
internal static string ModifiedOuterTableBasicStylePropertyName(WebControl control) {
// Verify that basic style properties are not set (not different than their defaults).
if (control.BackColor != Color.Empty) {
return "BackColor";
}
if (control.BorderColor != Color.Empty) {
return "BorderColor";
}
if (control.BorderWidth != Unit.Empty) {
return "BorderWidth";
}
if (control.BorderStyle != BorderStyle.NotSet) {
return "BorderStyle";
}
if (!String.IsNullOrEmpty(control.CssClass)) {
return "CssClass";
}
if (control.ForeColor != Color.Empty) {
return "ForeColor";
}
if (control.Height != Unit.Empty) {
return "Height";
}
if (control.Width != Unit.Empty) {
return "Width";
}
return String.Empty;
}
}
}
|