using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using SqlAdmin; namespace SqlWebAdmin { /// /// Summary description for Login. /// public class Login : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox ServerTextBox; protected System.Web.UI.WebControls.TextBox PasswordTextBox; protected System.Web.UI.WebControls.TextBox UsernameTextBox; protected System.Web.UI.WebControls.Button LoginButton; protected System.Web.UI.WebControls.Label ErrorLabel; protected System.Web.UI.WebControls.Label LogoutInfoLabel; protected System.Web.UI.WebControls.RequiredFieldValidator UsernameRequiredFieldValidator; protected System.Web.UI.WebControls.RequiredFieldValidator ServerRequiredFieldValidator; protected System.Web.UI.WebControls.Label LoginInfoLabel; public Login() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { if (ServerTextBox.Text != null && ServerTextBox.Text.Length == 0) { ServerTextBox.Text = Environment.MachineName; } if (UsernameTextBox.Text != null && UsernameTextBox.Text.Length == 0) { UsernameTextBox.Text = "sa"; } ErrorLabel.Visible = false; LogoutInfoLabel.Visible = false; LoginInfoLabel.Visible = false; if (Request["error"] != null) { switch (Request["error"]) { case "sessionexpired": ErrorLabel.Text = "Your session has expired or you have already logged out. Please enter your login info again to reconnect."; break; case "userinfo": ErrorLabel.Text = "Invalid username and/or password, or server does not exist.
Also, please ensure that SQL Server Authentication is enabled on the server."; break; default: ErrorLabel.Text = "An unknown error occured."; break; } ErrorLabel.Visible = true; } else if (Request["action"] == "logout") { LogoutInfoLabel.Visible = true; } else { LoginInfoLabel.Visible = true; } } 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 /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.LoginButton.Click += new System.EventHandler(this.LoginButton_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void LoginButton_Click(object sender, System.EventArgs e) { if (!IsValid) return; SqlServer server = new SqlServer(ServerTextBox.Text, UsernameTextBox.Text, PasswordTextBox.Text); if (server.IsUserValid()) { // Create cookie and add session information to it HttpCookie cookie = new HttpCookie("WebDataAdministrator"); cookie.Values.Add("username", server.Username); cookie.Values.Add("password", server.Password); cookie.Values.Add("server", server.Name); Response.Cookies.Add(cookie); Response.Redirect("databases.aspx"); } else { ErrorLabel.Visible = true; ErrorLabel.Text = "Invalid username and/or password, or server does not exist.
Also, please ensure that SQL Server Authentication is enabled on the server."; } } } }