我在ACCESS数据库中保存用户名密码,我想使用FROMS验证,但网上怎么都说得那样简单啊,都看不明白,有没有人能详细说一下呢?
解决方案 »
- jQuery 序列化表单提交出错
- gridview中取值的问题
- 让按钮根据条件的不同显示颜色`!!!!!!`~~~~~
- 一个关于GridView的问题
- UPDATE的问题,值取不到【紧急】
- 急问!如何在dataTextField绑定多行数据啊?
- 如何取得自定义web控件中的两个下拉框的值?
- 哪有C#的代码自动生成器
- 问一个简单SQL语句※※※※※※※※※※※※※※※※※※※在线等
- 散分:请问软件公司的开发模式:如何分块开发最后在组成一个整个的系统?
- 最后70分全拿出来了,问一个用dvi+css再加一些js脚本的html页面(附源码),如何转到aspx页面!
- 当鼠标移到treeview的节点上时 如何不让它高亮显示?在线等高手了,分不够再加
如果应用程序的根目录中有 Web.config 文件,请打开该文件。如果应用程序的根文件夹中没有 Web.config 文件,请创建一个名为 Web.config 的文本文件,并在其中添加下列元素: 复制代码
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web> </system.web>
</configuration>
在 system.web 元素中,创建一个 authentication 元素,并将它的 mode 属性设置为 Forms,如下面的示例所示: 复制代码
<system.web>
<authentication mode="Forms"> </authentication>
</system.web>
在 authentication 元素中,创建一个 forms 元素,并设置下列属性:loginUrl 设置为“Logon.aspx”。Logon.aspx 是 ASP.NET 在找不到包含请求内容的身份验证 Cookie 的情况下进行重定向时所使用的 URL。name 设置为“.ASPXFORMSAUTH”。这是为包含身份验证票证的 Cookie 的名称设置的后缀。 复制代码
<system.web>
<authentication mode="Forms">
<forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH"> </forms>
</authentication>
</system.web>
在 system.web 元素中,创建一个 authorization 元素。 复制代码
<system.web>
<authentication mode="Forms">
<forms loginUrl="Logon.aspx" name=".ASPXFORMSAUTH">
</forms>
</authentication>
<authorization> </authorization>
</system.web>
在 authorization 元素中,创建一个 deny 元素,并将其 users 属性设置为“?”。这是指定将拒绝未通过身份验证的用户(由“?”表示)访问该应用程序中的资源。 复制代码
<system.web>
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name=".ASPXFORMSAUTH">
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
保存并关闭 Web.config 文件。创建登录页
当用户从网站请求任何页时,如果他们此前未通过身份验证,将被重定向到名为 Logon.aspx 的页。您之前在 Web.config 文件中指定了该文件名。 Logon.aspx 页收集用户凭据(电子邮件地址和密码)并对它们进行身份验证。如果用户成功通过身份验证,登录页会将用户重定向到他们最初所请求的页。在本示例中,有效凭据被硬编码到页代码中。创建登录页
在应用程序根文件夹中创建一个名为 Logon.aspx 的 ASP.NET 页。将下面的标记和代码复制到该页中:Visual Basic 复制代码
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %><script runat="server">
Sub Logon_Click(ByVal sender As Object, ByVal e As EventArgs)
If ((UserEmail.Text = "[email protected]") And _
(UserPass.Text = "37Yj*99Ps")) Then
FormsAuthentication.RedirectFromLoginPage _
(UserEmail.Text, Persist.Checked)
Else
Msg.Text = "Invalid credentials. Please try again."
End If
End Sub
</script><html>
<head id="Head1" runat="server">
<title>Forms Authentication - Login</title>
</head>
<body>
<form id="form1" runat="server">
<h3>
Logon Page</h3>
<table>
<tr>
<td>
E-mail address:</td>
<td>
<asp:TextBox ID="UserEmail" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="UserEmail"
Display="Dynamic"
ErrorMessage="Cannot be empty."
runat="server" />
</td>
</tr>
<tr>
<td>
Password:</td>
<td>
<asp:TextBox ID="UserPass" TextMode="Password"
runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate="UserPass"
ErrorMessage="Cannot be empty."
runat="server" />
</td>
</tr>
<tr>
<td>
Remember me?</td>
<td>
<asp:CheckBox ID="Persist" runat="server" /></td>
</tr>
</table>
<asp:Button ID="Submit1" OnClick="Logon_Click" Text="Log On"
runat="server" />
<p>
<asp:Label ID="Msg" ForeColor="red" runat="server" />
</p>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %><script runat="server">
void Logon_Click(object sender, EventArgs e)
{
if ((UserEmail.Text == "[email protected]") &&
(UserPass.Text == "37Yj*99Ps"))
{
FormsAuthentication.RedirectFromLoginPage
(UserEmail.Text, Persist.Checked);
}
else
{
Msg.Text = "Invalid credentials. Please try again.";
}
}
</script>
<html>
<head id="Head1" runat="server">
<title>Forms Authentication - Login</title>
</head>
<body>
<form id="form1" runat="server">
<h3>
Logon Page</h3>
<table>
<tr>
<td>
E-mail address:</td>
<td>
<asp:TextBox ID="UserEmail" runat="server" /></td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="UserEmail"
Display="Dynamic"
ErrorMessage="Cannot be empty."
runat="server" />
</td>
</tr>
<tr>
<td>
Password:</td>
<td>
<asp:TextBox ID="UserPass" TextMode="Password"
runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
ControlToValidate="UserPass"
ErrorMessage="Cannot be empty."
runat="server" />
</td>
</tr>
<tr>
<td>
Remember me?</td>
<td>
<asp:CheckBox ID="Persist" runat="server" /></td>
</tr>
</table>
<asp:Button ID="Submit1" OnClick="Logon_Click" Text="Log On"
runat="server" />
<p>
<asp:Label ID="Msg" ForeColor="red" runat="server" />
</p>
</form>
</body>
</html>
此页包含用于收集用户信息的 ASP.NET 服务器控件和一个复选框,当用户单击该复选框时,他们的登录凭据将保存下来。“登录”按钮的 Click 处理程序包含对照硬编码的值来检查用户的电子邮件地址和密码的代码。(该密码是强密码,包含各种非字母字符,且至少为八个字符长。)如果用户的凭据正确,代码将调用 FormsAuthentication 类的 RedirectFromLoginPage 方法,并传递用户名和一个来源于复选框的布尔值,该值指示是否将身份验证票证保存为 Cookie。此方法将用户重定向到最初所请求的页。如果用户的凭据不匹配,将显示一条错误信息。请注意,该页会导入包含 FormsAuthentication 类的 System.Web.Security 命名空间。创建默认页
对于本示例,您将在应用程序根文件夹中创建一个 ASP.NET 页。由于您在配置文件中指定拒绝所有未通过身份验证的用户访问应用程序的 ASP.NET 资源(包括 .aspx 文件,但不包括静态文件,例如 HTML 文件或包括图像、音乐等在内的多媒体文件),因此,当用户请求该页时,Forms 身份验证将检查用户的凭据,并在必要的时候将用户重定向到登录页。您创建的页还将允许用户注销,以清除他们的已保存身份验证票证 (Cookie)。创建默认页
在应用程序根文件夹中创建一个名为 Default.aspx 的 ASP.NET 页。将下面的标记和代码复制到该页中:Visual Basic 复制代码
<%@ Page Language="VB" %>
<html>
<head>
<title>Forms Authentication - Default Page</title>
</head><script runat="server">
Sub Page_Load(ByVal Src As Object, ByVal e As EventArgs)
Welcome.Text = "Hello, " & Context.User.Identity.Name
End Sub Sub Signout_Click(ByVal sender As Object, ByVal e As EventArgs)
FormsAuthentication.SignOut()
Response.Redirect("Logon.aspx")
End Sub
</script><body>
<h3>
Using Forms Authentication</h3>
<asp:Label ID="Welcome" runat="server" />
<form id="Form1" runat="server">
<asp:Button ID="Submit1" OnClick="Signout_Click"
Text="Sign Out" runat="server" /><p>
</form>
</body>
</html>
C# 复制代码
<%@ Page Language="C#" %>
<html>
<head>
<title>Forms Authentication - Default Page</title>
</head><script runat="server">
void Page_Load(object sender, EventArgs e)
{
Welcome.Text = "Hello, " + Context.User.Identity.Name;
} void Signout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("Logon.aspx");
}
</script><body>
<h3>
Using Forms Authentication</h3>
<asp:Label ID="Welcome" runat="server" />
<form id="Form1" runat="server">
<asp:Button ID="Submit1" OnClick="Signout_Click"
Text="Sign Out" runat="server" /><p>
</form>
</body>
</html>
此页显示用户的已通过身份验证的标识,该标识是由 FormsAuthentication 类设置的,并作为 Context.User.Identity.Name 属性在 ASP.NET 页中提供。“注销”按钮的 Click 处理程序包含具有如下作用的代码:调用 SignOut 方法以清除用户标识并移除身份验证票证 (Cookie)。然后将用户重定向到登录页。
forms.aspx 登录页面
users 文件夹 登录后访问页面,拒绝匿名用户
2\web.config配置
<authentication mode="Forms">
<forms defaultUrl="users/?" loginUrl="forms.aspx" name ="test" path ="/" protection ="All" timeout ="60" />
</authentication>3\在这个节点 </configuration> 前面加入以下内容
<location path ="users">
<system.web>
<authorization>
<deny users ="?"/>
</authorization>
</system.web>
</location>4\forms.aspx UI部分
username:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
password:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br />
<asp:CheckBox ID="ckb" runat="server" />
<asp:Button ID="btn" runat="server" Text="登录" /></div>forms.vb 部分
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click Dim dsn As String = "provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("~/app_data/db1.mdb")
Dim conn As New System.Data.OleDb.OleDbConnection(dsn)
Dim cmd As OleDbCommand = conn.CreateCommand
With cmd
.CommandText = "select count(id) from userinfo where usr=? and pwd=?"
.Parameters.Add("@usr", OleDbType.VarChar).Value = TextBox1.Text
.Parameters.Add("@pwd", OleDbType.VarChar).Value = TextBox2.Text
End With
Dim result As Integer
Try
conn.Open()
result = System.Convert.ToInt32(cmd.ExecuteScalar()) If result > 0 Then System.Web.Security.FormsAuthentication.SetAuthCookie(TextBox1.Text, ckb.Checked)
Response.Redirect("users")
Else
Page.ClientScript.RegisterStartupScript(GetType(String), "msg", "<script>alert('用户名密码错误')</script>")
End If Catch ex As Exception
Response.Write(ex.Message.ToString) Finally
conn.Dispose()
cmd.Dispose()
End Try End Sub5\users目录中随便放一个文件夹这样应该比较清楚了吧.