ashx登陆

denglu.aspx.cs文件中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

public partial class denglu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines("password.txt");

string username = txtUserName.Value;
string password = passwordTxt.Value;

for (int i = 0; i < lines.Length; i++)
{

string[] line = lines[i].Split(' ');
if (username == line[0] && password == line[1])
{
htmlLabel1.InnerText = "登陆成功";
return;
}

}

htmlLabel1.InnerText = "登陆失败";

}
}

denglu.aspx文件中:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="denglu.aspx.cs" Inherits="denglu" %>
<html xmlns="
http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style3
{
width: 129px;
}
.style4
{
width: 159px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div >
<table align="center" border="1px" style="width: 35%" >

<tr><td class="style3">用户名:</td><td class="style4"><input type="text" id="txtUserName" runat="server"/></td></tr>

<tr><td class="style3">密 码:</td><td class="style4"><input type="text" id="passwordTxt" runat="server" /></td></tr>

<tr>
<td class="style3"><asp:Button ID="Button1" runat="server" Text="登陆" onclick="Button1_Click" /> <input type="button" id="btn1" value="html登陆" runat="server" onserverclick="Button1_Click"/></td>
<td class="style4"><label id="htmlLabel1" runat="server"></label></td>
</tr>
</table>
</div>
</form>
</body>
</html>

注意:

把password.txt文件放到C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\这个文件夹下,password.txt文件的编码方式为UTF-8,否则文件中的中文不能进行比较。

你可能感兴趣的:(sh)