Asp.Net Form身份验证


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 />
    密 &nbsp;&nbsp; 码:
        <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>

Asp.Net Form身份验证 - 0 - 圣·旋风    <sessionState mode="InProc" timeout="30"/>

Asp.Net Form身份验证 - 0 - 圣·旋风</system.web>

***.aspx.cs文件中

Session.Timeout = 1;


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值