Asp.Net Form身份验证
2012-09-02 20:25
183 查看
2009-12-12 11:32:18| 分类:.net基础|字号 订阅
Web.Config代码
<?xml version="1.0"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
\Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings>
</appSettings>
<connectionStrings/>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<!--自己写的配置文件-->
<compilation debug="true">
<!--解决当浏览器端禁用Cookie时-->
<!--自己写的配置文件 结束-->
<assemblies>
<add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<!--自己写的配置文件-->
<authentication mode="Forms">
<forms name=".SomeTsteAuth" loginUrl="login.aspx" defaultUrl="LoginSuccess.aspx" timeout="10">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
<!--加上此代码即可访问images文件夹-->
<location path="images">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<!--加上此代码即可访问验证码文件"ValidateCode.aspx"文件-->
<location path="ValidateCode.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
</configuration>
**************************************Login.aspx代码******************************************
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>登录</title>
<script>
function checkSubmit()
{
if(document.admininfo.TextBox1.value==""){
alert("用户名不能为空!");
document.admininfo.TextBox1.focus();
return false;
}
if(document.admininfo.TextBox2.value==""){
alert("密码不能为空!");
document.admininfo.TextBox2.focus();
return false;
}
}
</script>
</head>
<body>
<form id="admininfo" runat="server" onSubmit="return checkSubmit();">
<div>
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
密 码:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" /></div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
******************************************Login.aspx.cs代码***********************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using MODEL;
public partial class Login : System.Web.UI.Page
{
UserMessage user;
protected void Page_Load(object sender, EventArgs e)
{}
//登录事件
protected void Button1_Click(object sender, EventArgs e)
{
if (userName == "名字" && userPwd == "密码")
{
//产生凭证
FormsAuthentication.RedirectFromLoginPage("AdminName", false);
//如果需要重新指定路径则加如下语句
//Response.Redirect("LoginSuccess.aspx");
}
}
}
******************************************退出事件(注销票据)******************************************
//注销票
FormsAuthentication.SignOut();
//返回login.aspx页面
Response.Redirect("login2.aspx");
******************************************设置session过期时间******************************************
修改web.config配置
<system.web>
<sessionState mode="InProc" timeout="30"/>
</system.web>
***.aspx.cs文件中
Session.Timeout = 1;
相关文章推荐
- ASP.NET在IE9,IE10,IE11中Form表单身份验证失效问题解决方法
- ASP.NET在IE10,IE11中Form表单身份验证失效问题解决方法
- asp.net 身份验证-Form 身份验证
- ASP.NET身份验证——Form身份认证
- ASP.NET 中通过Form身份验证 来模拟Windows 域服务身份验证的方法
- ASP.NET在IE10中Form表单身份验证失效问题解决方法
- asp.net form 身份验证--不使用membershipprovider和rolesprovider(二)
- Asp.net中的Form身份验证
- Asp.net的身份验证 Form
- asp.net基于FORM的身份验证
- ASP.NET Form身份验证方式详解
- ASP.NET在IE9,IE10,IE11中Form表单身份验证失效问题解决方法
- C#、ASP.NET 环境下如何在 form 身份验证User.Identity中携带像 UserID 这样的信息
- ASP.NET Form身份验证 css和js无法正常显示问题
- ASP.NET MVC Form身份验证
- ASP.net基于窗体的身份验证
- Use Windows Authentication in ASP.NET 2.0/ASP.NET 2.0 中的 Windows 身份验证
- 一起谈.NET技术,也谈ASP.NET 中的身份验证
- asp.net中的窗体身份验证(完整篇之五:退出登录)
- ASP.Net 2.0 窗体身份验证机制-转+自己代码注释示例与更详细的说明