using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Text; using System.Text.RegularExpressions; /* ===================================================================== File: Login.aspx.cs for Adventure Works Cycles Storefront Sample Summary: Authenticates the user Date: June 16, 2003 --------------------------------------------------------------------- This file is part of the Microsoft SQL Server Code Samples. Copyright (C) Microsoft Corporation. All rights reserved. This source code is intended only as a supplement to Microsoft Development Tools and/or on-line documentation. See these other materials for detailed information regarding Microsoft code samples. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. ======================================================= */ namespace Microsoft.Samples.SqlServer { public partial class Login : System.Web.UI.Page { // public Login() { // Page.Init += new System.EventHandler(Page_Init); // } //******************************************************* // // The LoginButton_Click event is used on this page to // authenticate a customer's supplied username/password // credentials against a database. // // If the supplied username/password are valid, then // the event handler adds a cookie to the client // (so that we can personalize the home page's welcome // message), migrates any items stored in the user's // temporary (non-persistent) shopping cart to their // permanent customer account, and then redirects the browser // back to the originating page. // //******************************************************* private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Login1.Focus(); } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { } bool IsValidEmail(string strIn) { // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } void OnBeforeLogin(object sender, CancelEventArgs e) { if (!IsValidEmail(Login1.UserName)) { Login1.InstructionText = "Enter a valid e-mail address."; Login1.InstructionTextStyle.ForeColor = System.Drawing.Color. RosyBrown; e.Cancel = true; } else { Login1.InstructionText = String.Empty; } } protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { // Only attempt a login if all form fields on the page are valid if (Page.IsValid == true) { Login1.InstructionText = ""; String previousLogin = System.Web.HttpContext.Current.User. Identity.Name; // Save old ShoppingCartID ShoppingCartDB shoppingCart = new ShoppingCartDB(); String tempCartID = shoppingCart.GetShoppingCartId(); // Attempt to Validate User Credentials using CustomersDB CustomersDB accountSystem = new CustomersDB(); CustomerDetails customerDetails = accountSystem.Login(Login1. UserName, Login1. Password); String customerId = (customerDetails == null) ? null : customerDetails.CustomerID; if (customerId != null) { if (previousLogin.Length == 0) // Migrate any existing shopping cart items into the permanent shopping cart shoppingCart.MigrateCart(tempCartID, customerId); // Store the user's fullname in a cookie for personalization purposes String fullName = customerDetails.FirstName + " " + customerDetails.LastName; UnicodeEncoding unicode = new UnicodeEncoding(); Response.Cookies["AdventureWorks_FullName"].Value = Convert. ToBase64String(unicode. GetBytes(fullName)); // Make the cookie persistent only if the user selects "persistent" login checkbox if (Login1.RememberMeSet == true) { Response.Cookies["AdventureWorks_FullName"].Expires = DateTime. Now. AddMonths(1); } // Redirect browser back to originating page FormsAuthentication.RedirectFromLoginPage(customerId, Login1. RememberMeSet); } else { Login1.InstructionText = "Login Failed!"; } } } #endregion } }