📄 login.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 System.Web.Security;
using System.Configuration;
namespace AWC.Reporter.Web
{
/// <summary>
/// If Forms Authentication is enabled, you should be redirected automatically to this page
/// when you request MyOrders.aspx
/// </summary>
public class Login : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblStatus;
protected System.Web.UI.WebControls.LinkButton lnkLogin;
protected System.Web.UI.WebControls.RequiredFieldValidator requiredFieldValidator;
protected System.Web.UI.WebControls.RangeValidator rangeValidator;
protected System.Web.UI.WebControls.Panel pnlLogin;
protected System.Web.UI.WebControls.ValidationSummary validationSummary;
protected System.Web.UI.WebControls.TextBox txtCustomerId;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lnkLogin.Click += new System.EventHandler(this.lnkLogin_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// Queries the database to find out whether a customer with the given id exists
/// </summary>
/// <param name="customerID"></param>
/// <returns></returns>
private bool IsAuthentic(string customerID)
{
SqlConnection connection = new SqlConnection();
SqlDataReader reader = null;
try
{
// see whether a customer match is found in the Customer table
string sql = "SELECT CustomerID FROM Customer WHERE CustomerID=" + customerID;
connection.ConnectionString = ConfigurationSettings.AppSettings[Constants.CONFIG_CONNECT_STRING];
SqlCommand sqlCmd = new SqlCommand(sql, connection);
sqlCmd.Connection.Open();
reader = sqlCmd.ExecuteReader();
return reader.HasRows;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
reader.Close();
connection.Close();
}
}
private void lnkLogin_Click(object sender, System.EventArgs e)
{
// ID of a customer
string customerID = txtCustomerId.Text;
// attempt to Validate User Credentials using CustomersDB
if (IsAuthentic(customerID))
// redirect browser back to originating page
FormsAuthentication.RedirectFromLoginPage(customerID, false);
else
// login failed
lblStatus.Text = "Login Failed!";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -