一个简单的用ASP.NET/C#开发的组件化Web应用程序(附源代码)

==============================================================================
1)创建一个类来处理用户登录,将该类编译成一个装配件(assembly),并发布到站点的bin目录下。

编辑C#源程序文件class.cs:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;

namespace Component
{
public class CheckLogin
{
public CheckLogin() : base()
{
}

//输入工号和密码,返回用户名
public string isMember(string userName, string password)
{
// 定义SQL语句
string sql = "select ltrim(rtrim(nvcLName))+ltrim(rtrim(nvcFName)) as EmployeeName from [user] where workerNo = '" + userName + "' and [password] = '" + password + "'";

// 定义SQL连接
SqlConnection cn = new SqlConnection("server=server_sql;uid=sa;pwd=;database=lhzz");

// 定义SQL命令并打开连接
SqlCommand cmd = new SqlCommand(sql, cn);
cn.Open();

// 定义SQL数据读取器、执行数据读取、返回特定标题列的内容
SqlDataReader sdr;
sdr = cmd.ExecuteReader();
string name="";
if (sdr.Read())
{
name=sdr["EmployeeName"].ToString();
}
return name;
}
}
}

用csc命令将class.cs编译为Component.dll,然后把该文件复制到站点根目录的bin子目录下。这样,一个装配件的发布就完成了。

==================================================================================
2)建立登录页(login.aspx)。当用户尚未登录就访问主页(default.aspx)时,会被重定向到登录页。

源程序如下:

1@ Import Namespace="Component" 
2``` //引入名称空间——通过这样可以在程序中使用Component.dll中的类   

@ Import Namespace="System.Web.Security"

欢迎来到我的主页!

请登录。
用户:

密码:<asp:textbox id="inputPassword" runat="server" text="" textmode="Password" width="200px"></asp:textbox>

     
<asp:button onclick="SubmitBtn_Click" runat="server" text="登录"></asp:button>

<asp:label id="labelMessage" runat="server" style="color:red"></asp:label>

``` ========================= 3)建立首页(default.aspx) =========================

源程序如下:

1@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" 
 1<html>
 2<head>
 3<script language="C#" runat="server">   
 4protected void Page_Load(Object Src, EventArgs E)   
 5{   
 6labelUserName.Text = User.Identity.Name; // 获得经过验证的用户名   
 7}   
 8protected void Button_OnClick(Object Src, EventArgs E) // 注销并重定向到login.aspx   
 9{   
10FormsAuthentication.SignOut();   
11Response.Redirect("login.aspx");   
12}   
13</script>
14<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
15<title>首页</title>
16</head>
17<body>
18<form action="" method="post" name="form1" runat="server">
19<table border="0" width="90%">
20<tr>
21<td width="90%">欢迎你,<asp:label id="labelUserName" runat="server"></asp:label> !   
22</td>
23<td width="10%"><asp:button id="buttonSignout" onclick="Button_OnClick" runat="server" text="注销"></asp:button></td>
24</tr>
25</table>
26</form>
27
28====================   
294)配置web.config文件   
30==================== 
31
32设置身份验证模式为“表单”模式: 
33
34<configuration>
35<system.web>
36<authentication mode="Forms">
37<forms loginurl="login.aspx" name="login" protection="All" timeout="60"></forms>
38</authentication>
39<authorization>
40<deny users="?"></deny>
41</authorization>
42</system.web>
43</configuration></body></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus