form验证学习

allow  表示允许

deny 拒绝

?匿名用户 * 所有用户

authcation  在所有的config文件中只能出现一次

在子目录中配置 authzation

 

 

 

<authorization>
<allow user='admin'>//允许admin权限的用户
<deny user='*'>//拒绝所有
</authorization>
代码
在webconfig中 首先

<authentication  mode="Forms" >
        
<forms loginUrl="login.aspx" ></forms>//指定整个网站都必须登陆
      </authentication>
      
<authorization >
        
<deny users="?"/>//拒接匿名用户
      </authorization>



User.Identity.Name  //获得刚才保存的用户名

 

FormsAuthentication.RedirectFromLoginPage(this.txt_username.Text, false);//用户名密码输入正确登陆时 保存下用户名 然后跳转到

用户一开始打开的页面



FormsAuthenticationTicket authTicket 
= FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
                
string role = authTicket.UserData; //获得角色
System.Web.Security.FormsAuthentication.SetAuthCookie(this.Txt_UserName.Text,false);

 

     Response.Redirect(
"Default.aspx");//不管登陆之前看的什么页面 登陆后都转到默认首页

 



 

Web.config 的设置将作用于所在目录的所有文件及其子目录下的所有东东(继承:子随父姓)
子目录下的 Web.config 设置将覆盖由父目录继承下来的设置(覆盖:县官不如现管)

 

代码
<?xml version="1.0" encoding="utf-8"?> 
<configuration>
    
<system.web>
      
<authorization>
        
<allow users="*"/>//允许所有用户
      </authorization>
    
</system.web>
</configuration>
//在子目录下设置webconfig 使得本文件夹下的所有文件任何人都可以访问

 

代码
<?xml version="1.0" encoding="utf-8"?> 
<configuration>
    
<system.web>
      
<authorization>
        
<allow users="admin"/> //本目录下只允许用户名为admin的进入看本文件夹的文件
      </authorization>
    
</system.web>
</configuration>

 

 

 

posted @ 2009-12-31 17:41  任飞儿  阅读(142)  评论(0编辑  收藏  举报