//判断用户名是否正确。。
string User,Pass;
        User=user.Text;
        Pass=pass.Text;
        Iindex iindex = new index();
        if (User == ""){ Label1.Text = "管理员用户名不能为空!"; }
        else
        {
            if (Pass == "") { Label1.Text = "管理员密码不能为空!"; }
            else
            {
                if (iindex.admingo(User, Pass))
                { FormsAuthentication.RedirectFromLoginPage(User, false); } //正确。允许访问保护文件。
                    
                else
                {
                    Label1.Text = "登陆失败.请确认用户和密码是否输入正确!";
                }
            }
        }



HttpContext.Current.User.Identity.Name; //得到当前登陆的用户名。。
System.Web.Security.FormsAuthentication.SignOut();  注销登陆 




web.config文件配置。

<?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
 <connectionStrings>
  <add name="Data" connectionString="Data Source=127.0.0.1;Initial Catalog=Sf00654;User ID=sa;Password=4501550"
   providerName="System.Data.SqlClient" />
 </connectionStrings>
 <appSettings/>
 <system.web>
  <compilation debug="true"/>
    <!--=========================================================================================设置为窗体身份验证方式...-->
    <authentication mode="Forms" >
      <forms loginUrl="Admin/index.aspx" protection="All" timeout="360" name=".ASPX00654" requireSSL="false" cookieless="UseDeviceProfile" defaultUrl="admin/admin_index.aspx">
      </forms>
    </authentication>
    <!--=========================================================================================-->
  </system.web>
  <location path="Admin">  <!--受保护的文件夹-->
    <system.web>
      <authorization>
        <deny users="?"></deny>
        <!--这是一个授权节,此节设置应用程序的授权策略。可以允许(allow)或拒绝(deny)不同的用户或角色访问应用程序资源。
        通配符: "*" 表示任何人,"?" 表示匿名(未经身份验证的)用户。看我们上面的设置知道,这样配置就是拒绝未经身份验证的用户访问。-->
      </authorization>
    </system.web>
  </location>
  <!--=========================================================================================-->
</configuration>