Tạo Web form dangnhap.aspx cho phép người dùng nhập vào tên đăng nhập và mật khẩu Khi kích nút Đăng nhập sẽ kiểm tra tên đăng nhập, và mật khẩu có tồn tại trong cơ

Một phần của tài liệu Bài tập kỹ thuật thương mại điện tử potx (Trang 34 - 37)

khẩu. Khi kích nút Đăng nhập sẽ kiểm tra tên đăng nhập, và mật khẩu có tồn tại trong cơ sở dữ liệu, nếu có chuyển đến trang loaihang.aspx, ngược lại quay trở về trang đăng nhập, và thông báo đăng nhập lại hay đăng ký. Trên trang dangnhap.aspx, có liên kết đến trang dangky.aspx

Hướng dẫn:

- Thiết lập cần thiết trong tập tin Web.config

- Bổ sung một tập tin lớp mới Users.cs vào ứng dụng bao gồm hai phương thức kiểm tra đăng nhập AuthenticateUser(username, password) và đăng ký người dùng AddUser(username, password, hoten, diachi, email)

using System.Data;

using System.Data.SqlClient; using System.Configuration; public class Users

{

public bool AuthenticateUser(string username, string password) { bool authenticated;

string connString = ConfigurationSettings.AppSettings["con"]; SqlConnection con = new SqlConnection(connString);

string query = "select * from khachhang where tendangnhap='" + username + "' and matkhau='" + password + "'";

SqlCommand cmd = new SqlCommand(query, con); con.Open();

SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { authenticated = true; } else { authenticated = false; } reader.Close(); con.Close();

Bài tập thực hành Kỹ thuật thương mại điện tử

con.Dispose(); return authenticated; }

public bool AddUser(string username, string password, string fullname, string address, string email)

{

//Bổ sung hàm này }

}

- Gọi hai phương thức này trên Web form dangnhap.aspx và dangky.aspx

Web form Dangnhap.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dangnhap.aspx.cs" Inherits="Dangnhap" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body>

<form id="form1" runat="server"> <div>

USER LOG IN <br />

<asp:Label ID="lblMessage" runat="server"></asp:Label><br /> Username

<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /> <br />

Password

<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> <br />

<br />

<asp:Button ID="Button1" runat="server" Text="Log in" OnClick="login_Click"/> </div> </form> </body> </html> Dangnhap.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; 35

using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; (adsbygoogle = window.adsbygoogle || []).push({});

public partial class Dangnhap : System.Web.UI.Page {

protected void login_Click(object sender, System.EventArgs e) {

if (Page.IsValid) {

Users users = new Users(); bool auth;

auth = users.AuthenticateUser(txtUserName.Text, txtPassword.Text); if (auth)

{

// đăng nhập thành công, hãy tạo authentication

// cookie và chuyển đến trang dangnhap.aspx hay // chuyển đến trang khác

FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); }

else {

lblMessage.Text = "Account information was incorrect. Please try again!"; // If third try, display "Access Denied" page.

if (System.Convert.ToInt32(ViewState["Tries"])> 1) Response.Redirect("Deni.htm");

else

// Otherwise, increment number of tries.

ViewState["Tries"] = System.Convert.ToInt32(ViewState["Tries"]) + 1; }

} else {

lblMessage.Text = "Missing some fields. Please try again."; }

} }

4.

- Xây dựng các Web form loại hàng, mặt hàng và lưu thông tin giỏ hàng vào cơ sở dữ liệu tương tự bài thực hành 5, nhưng thông tin giỏ hàng cần lưu trữ cùng với thông tin của khách hàng vào Table DONHANG và DHCHITIET

Bài tập thực hành Kỹ thuật thương mại điện tử

5.

- Xây dựng các Web form loại hàng, mặt hàng và lưu thông tin giỏ hàng vào Session tương tự bài thực hành 6

- Ở trang Giohang.aspx, khi khách hàng kích vào nút Thanh toán, thông tin giỏ hàng từ Session cần lưu trữ cùng với thông tin của khách hàng vào Table DONHANG và DHCHITIET

6. Sử dụng đối tượng Application để hiển thị số lần người dùng truy cập Website trên trang chủ của Web site

Một phần của tài liệu Bài tập kỹ thuật thương mại điện tử potx (Trang 34 - 37)