Asp.Net从零开始学-23

来源:互联网 发布:输入手机号定位软件 编辑:程序博客网 时间:2024/05/22 02:57

 <?xml version="1.0" encoding="utf-8" ?>
<!--
    说明:
    1.所有的配置必须放在<configuration>和</configuration>标记之中
    2.<appSetting>和</appSetting>之间是自定义配置,通常用来设置一些常量,
    3.<system.web>和</system.web>之间的标记是关于整个应用程序的配置
    4<location>和</location>是一个区域标记一般定义于某一个目录
    -->
<configuration>
<appSettings>
 <add key="con" value="server=.;database=Northwind;uid=sa;pwd=12345678;"></add>
 <add key="select" value="select * from Employees"></add>
</appSettings>

    <!--
    SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
 con.Open();
 SqlCommand cmd=new SqlCommand (System.Configuration.ConfigurationSettings.AppSettings["select"],con);
 this.DataGrid1.DataSource=cmd.ExecuteReader();
 this.DataGrid1.DataBind();
    -->

  <system.web>
    <!-- <pages buffer="false"/>   数据缓冲以16k为一单元向用户断续发送  -->
    <!--<trace enabled="ture" pageOutput="true"-->

    <compilation
         defaultLanguage="c#"
         debug="true"
    />
    <!--是否显示错误代码-->

    <!--  自定义错误信息
          设置 customErrors mode="On" 或 "RemoteOnly" 以启用自定义错误信息,或设置为 "Off" 以禁用自定义错误信息。
          为每个要处理的错误添加 <error> 标记。
          "On" 始终显示自定义(友好的)信息。
          "Off" 始终显示详细的 ASP.NET 错误信息。
          "RemoteOnly" 只对不在本地 Web 服务器上运行的
           用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便
           不向远程客户端显示应用程序的详细信息。
    -->
    <customErrors defaultRedirect="error.aspx"
    mode="On"
    /> 
    <!--
  Windows   Windows身份验证作为默认的身份验证模式。用于任何形式的IIS身份验证
  Forms     机遇ASP.NET窗体的身份验证作为默认的身份验证模式
  Passport  MicrosoftPassport身份验证作为默认的身份验证模式
  None      没有身份验证。用于匿名用户和可以提供其自己的身份验证的应用程序
 -->

    <authentication mode="Forms">
  <forms name="authWeb" loginUrl="login.aspx" protection="All"></forms>
    </authentication>

    <!--
 name        None    用于身份验证的Cookie名称
 loginUrl    None    登录页URL如果没有身份验证Cookie,客户端被重定向到此URL
 protection  All     应用程序同时使用数据验证和加密来保护Cookie
             None    加密和验证都禁用
 timeout             一段时间(按分钟计),这段时间之后的身份验证Cookie将到期。默认值为30
 path                由应用程序发表的Cookie的路径,默认值是(/)
 -->
 <!--  授权
           此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
          应用程序资源。通配符: "*" 表示任何人,"?" 表示匿名
          (未经身份验证的)用户。
          allow允许  deny不允许
    -->
    <authorization>
        <deny users="?" /> <!-- 允许所有用户 -->
            <!--  <allow     users="[逗号分隔的用户列表]"
                             roles="[逗号分隔的角色列表]"/>
                  <deny      users="[逗号分隔的用户列表]"
                             roles="[逗号分隔的角色列表]"/>
            -->

    </authorization>
 </system.web>
</configuration>

                          如何保留Cookie
//存入Cookie中
//System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);

System.Web.Security.FormsAuthentication.RedirectFromLoginPage(TextBox1.Text,this.CheckBox1.Checked);
Response.Redirect("WebForm2.aspx");
//清除Cookie
System.Web.Security.FormsAuthentication.SignOut();

//MD5值加密
Response.Write(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text,"MD5"));

 <authentication mode="Forms" >
   <forms name="authCre" loginUrl="login.aspx" protection="All">
       <credentials passwordFormat="MD5">
                <user name="aaa" password="47BCE5C74F589F4867DBD57E9CA9F808"/>
                <user name="bbb" password="08F8E0260C64418510CEFB2B06EEE5CD"/>
       </credentials>
   </forms>
</authentication>
<authorization>
        <deny users="?" /> <!-- 禁止匿名用户 -->
</authorization>
private void Button1_Click(object sender, System.EventArgs e)
  {//使用MD5验证
   if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,this.TextBox2.Text))
   {
    System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);
    Response.Redirect("WebForm1.aspx");
   }
   else{
    Response.Write("error");
   }
 }

 

原创粉丝点击