---------------------------------學霸,學神,大牛,hacker請繞道de分割線-----------------------------------------------------------------------
嗯,這個可能比較簡單,原諒我這個學弱第一次做.net還查了好久的資料,所以貼出來,避免大家再走彎路了。
因為html的button按鈕不是服務器端控件,所以得做如下改變才能跳轉到后台。
<button type="submit" runat="server" onserverclick="Login" >登錄</button>
前台 login.aspx的代碼
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="admin_index" %> <!-- 這一行記得要變--> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 <html lang="en" class="no-js"> 5 6 <head> 7 8 <meta charset="utf-8"> 9 <title>homepage 后台登錄</title> 10 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 11 <meta name="description" content=""> 12 <meta name="author" content=""> 13 14 <!-- CSS --> 15 <link rel="stylesheet" href="assets/css/reset.css"> 16 <link rel="stylesheet" href="assets/css/supersized.css"> 17 <link rel="stylesheet" href="assets/css/style.css"> 18 19 <!-- HTML5 shim, for IE6-8 support of HTML5 elements --> 20 <!--[if lt IE 9]> 21 <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 22 <![endif]--> 23 24 </head> 25 26 <body> 27 28 <div class="page-container"> 29 <h1>教師主頁后台登錄</h1> 30 <form id="form" runat="server" method="post"> 31 <input type="text" name="username" placeholder="用戶名"/> 32 <input type="password" name="password" placeholder="密碼"/> 33 <button type="submit" runat="server" onserverclick="Login" >登錄</button> <!-- --> 34 <!-- 35 <asp:Button ID="Button" runat="server" Text="登 錄" OnClick="Login" /> 36 //--> 37 <div class="error"><span>+</span></div> 38 </form> 39 40 </div> 41 42 <!-- Javascript --> 43 <script src="assets/js/jquery-1.8.2.min.js"></script> 44 <script src="assets/js/supersized.3.2.7.min.js"></script> 45 <script src="assets/js/supersized-init.js"></script> 46 <script src="assets/js/scripts.js"></script> 47 48 </body> 49 50 </html>
前台做好后,就是從后台去html標簽的值了,用Request.Form["username"]根據其name屬性去取值。然后如果用戶名密碼正確則跳轉頁面並在session中放入一個值,若不正確,彈出提示。
后台login.aspx.cs代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.Security; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class admin_index : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 13 } 14 protected void Login(object sender, EventArgs e) 15 { 16 if (Request.Form["username"] == "123456" && Request.Form["password"] == "123456") 17 { 18 19 // FormsAuthentication.RedirectFromLoginPage(user, true);//使用.net的Security機制 20 Session["login"] = "OK"; //隨意在session中放入一個值 21 Response.Redirect("~/admin/Paper.aspx"); 22 23 } 24 else 25 { 26 Response.Write("<script>alert('請輸入正確的用戶和密碼!');</script>"); 27 } 28 29 30 } 31 }
下來就做后台驗證,就是如果沒有登錄就不能做相關的操作。
后台Honor.asp.cs代碼(登錄后隨意的一個頁面的后台)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class Honor : System.Web.UI.Page 9 { 10 protected void Page_Load(object sender, EventArgs e) 11 { 12 if (Session["login"] == null || Session["login"].ToString() == "" && Session["login"].ToString() != "OK") 13 { 14 15 Response.Write("<script>alert('沒有登錄,請登錄');window.top.location.href='/Login.aspx';</script>"); 16 17 18 } 19 20 } 21 }
上面通過對session中值的判斷來判斷用戶是否登錄。
這個驗證方式比較簡單,本來樓主想使用.net的Security機制,可是不會,就只好用這個了,所以有會的同學可以教我一下嘛哈哈!