asp.net 学习笔记
WEB自定义控件
Web.config
<!--
说明:
1.所有的配置都必须被放在<configuration>和</configuration>标记之中.
2.<appSettings>和</appSettings>之间是自定义配置,通常用来自己设置一些常量,Add添加常量,Key是常量的名称,
value是常量的值.
<appSettings>
<add key=\"con\" value=\"server=.;database=northwind;uid=sa;pwd=;\"></add>
</appSettings>
在程序中可以用System.Configuration.ConfigurationSettings.AppSettings[\"con\"]取值
SqlConnection con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[\"con\"]);
con.Open();
SqlCommand cmd=new SqlCommand(\"select * from employees\",con);
this.DataGrid1.DataSource=cmd.ExecuteReader();
this.DataGrid1.DataBind();
3.<system.web>和</system.web>之间的标记是关于整个应用程序的设置.
如 <pages buffer=\"true\"/> 使用页缓冲
4.<location>和</location>是一个区域标记.Path=\"aaa\"表示下面的设置只对该文件有效.
-->
customErrors设置(在<system.web>和</system.web>之间)
语法
<customErrors
defaultRedirect=\"url\"
mode=\"On|Off|RemoteOnly\">
<error statusCode=\"statuscode\" redirect=\"url\"/>
</customErrors>
身份验证和授权
身份验证类型: WINDOWS 描述: WINDOWS 身份难作为默认的身份验证模式.用于任何形式的IIS身份验证
身份验证类型: FORMS 描述: 基于APS.NET窗体的身份验证作为默认的身份验证模式
身份验证类型: PASSPORT 描述:Microsoft Passport身份验证作为默认的身份验证模式
身份验证类型: NONE 描述: 没有身份验证.用于匿名用户和可以提供其自己的身份验证的应用程序.
<configuration>
<system.web>
<authentication mode=\"Windows|Forms|Passport|None\">?
<forms name=\"name\" loginUrl=\"url\"
protection=\"All|NOne|Encryption\"
timeout=\"xx\" path=\"/\">?
<credentials passwordFormat=\"Clear|SHA1|MD5\"> /*Clear为明文密码*/
<user name=\"用户名\" password=\"密码\"/>
</credentials>
</forms>?
<passport redirectUrl=\"internal\"/>?
</authentication>
</system.web>
</configuration>
//基于forms先把IIS中该应用的匿名访问去掉
<forms>标记的属性
属性 选项 描述
name None 登录身份验证的Cookie名称
loginUrl None 登录页URL.如果没有身份验证Cookie,客户端将被重定向到此URL
protection ALL 应用程序同时使用数据验证和加密来保护Cookie
None 加密和验证都禁用
timeout 一段时间(按分钟计),这段时间之后身份验证Cookie将到期,默认值为30
path 由应用程序发布的Cookie的路径.默认值是反斜杠(/)
<authentication mode=\"Forms\">
<forms name=\"YourCookieName\" loginUrl=\"login.aspx\" protection=\"ALL\"></forms>
</authentication>
//授权
<authorization>
<allow users=\"?\"/> //<allow users=\"*\"/><!--允许所有用户 -->
<!--
<allow users=\"[逗号分隔的用户列表]\"
roles=\"[逗号分隔的角色列表]\"/>
<deny users=\"[逗号分隔的用户列表]\"
roles=\"[逗号分隔的角色列表]\"/>
-->
</authorization>
//login.aspx
登录代码
//连接数据库进行验证
if(true)//用户名是否合法
{
// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);// 指定的用户名写入到Cookie中(false临时Cookie,true永久Cookie)
// Response.Redirect(\"\");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//转到用户原访问的页
//如果为true,可用System..Web.Security.FormsAuthentication.SignOut();删除,以后又要求登录
}
else
{
Response.Write(\"用户不合法\");
}
//如果用户少的话,可以不用数据库,直接允许/拒绝用户
<authentication mode=\"Forms\"
<forms name=\"authCre\" loginUrl=\"login.aspx\" protection=\"ALL\">
<credentials passwordFormat=\"Clear\">
<user name=\"aaa\" password=\"aaa\"/>
<user name=\"bbb\" password=\"bbb\"/>
</credentials>
</forms>
</authentication>
登录代码
private void Button1_Click(object sender,System.EventArgs e)
{
if(System.Web.Security.FormsAuthentication.Authenticate(this.TextBox1.Text,This.TextBox2.Text)
{
// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,true);//此方法需重定向
// Response.Redirect(\"WebForm1.aspx\");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//此方法不需要重定向,直接转到原访问页
}
else
{
Response.Write(\"用户不合法\");
}
}
//授权时,通配符*表示任何人,?表示匿名

浙公网安备 33010602011771号