asp.net 学习笔记(2)
身份验证和授权
身份验证类型: 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("用户不合法");
}
}
//授权时,通配符*表示任何人,?表示匿名
Tag: web service
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。