Contoh berikut:
login.aspx HTML代码
1<%@ Halaman bahasa="c#" Codebehind="02Login.aspx.cs" AutoEventWireup="false" Inherits="身份验证._02Login" %>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisi//EN" >
3<HTML>
4 <KEPALA>
5 <title>02Masuk</title>
6 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
7 <meta name="CODE_LANGUAGE" Konten="C#">
8 <meta name="vs_defaultClientScript" content="JavaScript">
9 <meta name="vs_targetSchema" content=" http://schemas.microsoft.com/intellisense/ie5 ">
10</KEPALA>
11 <tubuh MS_POSITIONING="GridLayout">
12 <form id = "Form1" metode = "posting" runat = "server">
13 <FONT muka="宋体">
14 <TABEL id="Tabel1" style="Z-INDEX: 102; KIRI: 152 piksel; LEBAR: 446 piksel; POSISI: absolut; ATAS: 80 piksel; TINGGI: 72 piksel"
15 cellSpacing="1" cellPadding="1" width="446" border="1">
16 <TR>
17<TD>
18 <asp:label id="Label1" runat="server">Kemampuan untuk melakukan hal ini:</asp:label></TD>
19<TD>
20 <asp:textbox id="tbName" runat="server" Lebar="183px"></asp:textbox></TD>
21<TD>
22 <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" ErrorMessage="用户名不能为空!" ControlToValidate="tbName"></asp:requiredfieldvalidator></TD>
23</TR>
24 <TR>
25 <TD>
26 <asp:label id="Label2" runat="server">密码:</asp:label></TD>
27 <TD>
28 <asp:textbox id="tbPass" runat="server" Lebar="183px"></asp:textbox></TD>
29 <TD>
30 <asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" ErrorMessage="密码不能为空!" ControlToValidate="tbPass"></asp:requiredfieldvalidator></TD>
31</TR>
32 <TR>
33 <TD><FONT face="宋体">是否保存Cookie</FONT></TD>
34 <TD>
35 <asp:checkbox id="PersistCookie" runat="server"></asp:checkbox></TD>
36 <TD></TD>
37</TR>
38 </TABEL>
39 <asp:button id="btnLoginBetter" style="Z-INDEX: 101; KIRI: 288px; POSISI: absolut; ATAS: 240px"
40 runat="server" Lebar="78px" Text="登录"></asp:button>
41 <asp:HyperLink id="HyperLink1" style="Z-INDEX: 103; KIRI: 456px; POSISI: absolut; ATAS: 240px"
42 runat="server" NavigateUrl="Default.aspx">HyperLink</asp:HyperLink></FONT>
43 </bentuk>
44 </tubuh>
45</HTML>
login.aspx.cs di sini
private void btnLoginBetter_Click(pengirim objek, System.EventArgs e)
{
if (ini.tbNama.Teks == "admin" && ini.tbPass.Teks == "admin")
{
Tiket FormsAuthenticationTicket = new FormsAuthenticationTicket(1,this.tbName.Text,DateTime.Now,DateTime.Now.AddMinutes(30),this.PersistCookie.Checked,"User");//创建一个验证票据
string cookieStr = FormsAuthentication.Encrypt(tiket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookieStr);创建一个cookie,cookie名为web.config设置的名,值为加密后的数据cookieStr,
if (this.PersistCookie.Checked)//判断用户是否选中保存cookie
cookie.Kedaluwarsa = tiket.Kedaluwarsa;//获取cookie过期时间
cookie.Path = FormsAuthentication.FormsCookiePath;//设置cookie保存路径
Respon.Cookies.Add(cookie);
string strRedirect;
strRedirect = Permintaan["ReturnUrl"];//取出返回url
jika (strRedirect == nol)
strRedirect = "Default.aspx";
Respon.Redirect(strRedirect,true);
}
kalau tidak
{
Response.Write("<script>alert('帐号或密码错误!');self.location.href='02login.aspx'</script>");
}
}
Default.aspx HTML代码
<badan MS_POSITIONING="GridLayout">
<form id="Form1" metode="posting" runat="server">
<FONT muka="宋体">
<asp:Label id="Label1" style="Z-INDEX: 106; KIRI: 224px; POSISI: absolut; ATAS: 72px" runat="server">用户名称:</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 102; KIRI: 220px; POSISI: absolut; ATAS: 136px" runat="server">身份:</asp:Label>
<asp:Label id="lbUser" style="Z-INDEX: 103; KIRI: 350px; POSISI: absolut; ATAS: 79px" runat="server"></asp:Label>
<asp:Label id="lbSf" style="Z-INDEX: 104; KIRI: 355px; POSISI: absolut; ATAS: 133px" runat="server"></asp:Label>
<asp:Button id="btnLogout" style="Z-INDEX: 105; KIRI: 261px; POSISI: absolut; ATAS: 192px"
runat="server" Text="注销" Lebar="101px"></asp:Tombol></FONT>
</bentuk>
</tubuh>
后置代码
private void Page_Load(pengirim objek, System.EventArgs e)
{
this.lbUser.Text = Pengguna.Identitas.Nama;
if (Pengguna.IsInRole("Admin"))
this.lbSf.Teks = "Admin";
kalau tidak
this.lbSf.Text = "Pengguna";
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
ganti void OnInit yang dilindungi (EventArgs e)
{
//
// CODEGEN: dari ASP.NET Web dan dari Web ASP.NET.
//
Inisialisasi Komponen();
base.OnInit(e);
}
/**//// <ringkasan>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </ringkasan>
kekosongan pribadi InitializeComponent()
{
this.btnLogout.Klik += Sistem baru.EventHandler(ini.btnLogout_Click);
this.Load += Sistem baru.EventHandler(ini.Page_Load);
}
#endregion
private void btnLogout_Click(pengirim objek, System.EventArgs e)
{
FormsAuthentication.SignOut();//注销票
Response.Redirect("login.aspx",true);返回login.aspx页面
}
webconfig
<mode autentikasi="Formulir" >
<forms name=".SecurityDemo" loginUrl="login.aspx">//.SecurityDemo为cookie名,
</form>
</otentikasi>
<otorisasi>
<menyangkal pengguna="?"/> //拒绝所有匿名用户
<izinkan role="admins"/>//允许管理级别用户访问
</otorisasi>
sesi ASP.NET, sesi ASP.NET, dan sesi ASP.NET上所有页面都要验证才能访问,感觉有点迁强.但可以在web.config页面对指定的页面设置权限,设置代码如下
<jalur lokasi="admin.aspx">
<sistem.web>
<otorisasi>
<menolak pengguna="?" />
</otorisasi>
</sistem.web>
</lokasi>
如果只有几个页面设置如上代码,感觉还可以接受.但页面多了岂不是要把人累死呀..
不甚感激.
http://www.cnblogs.com/paleyyang/archive/2006/10/21/536147.html