惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
PCI Perspectives
PCI Perspectives
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
量子位
博客园_首页
S
SegmentFault 最新的问题
S
Secure Thoughts
F
Full Disclosure
H
Hacker News: Front Page
博客园 - 三生石上(FineUI控件)
U
Unit 42
H
Heimdal Security Blog
N
News and Events Feed by Topic
A
About on SuperTechFans
C
CERT Recently Published Vulnerability Notes
Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
The Hacker News
The Hacker News
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
N
News | PayPal Newsroom
Spread Privacy
Spread Privacy
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
云风的 BLOG
云风的 BLOG
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
人人都是产品经理
人人都是产品经理
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
L
LINUX DO - 热门话题
Google Online Security Blog
Google Online Security Blog
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
B
Blog RSS Feed

博客园 - somesongs

好多年没回到这个园子 有趣的多媒体 发布简短计划,备忘 Intelligencia.UrlRewriter, 解决404找不到文件的问题 在一个页面中调用另一个页面定义的函数 .net中,数据提交完毕后,刷新绑定控件,清空输入框的好办法,就是在时间函数的最后加入Response.Redirect(Request.FilePath); [转] Trace跟踪输出进行调试 Flash的全屏播放(转) flash中实现拖拽 Treeview中,递归生成从当前选中节点到根节点的全路径 .net从后台返回js的提示框 编写xmlhelper类【翻译】 [转]C#网络编程概述 【转】C#网络编程初探 只需一行代码实现增删查改,微软已经让我们很简单。谈AccessDataSource的使用。 document.body.clientHeight与document.documentElement.clientHeight 动态将Js代码写入到Head标签中 Asp.net中删除前的提示信息 location和history的详细设置
forms验证如此简单
somesongs · 2009-02-19 · via 博客园 - somesongs

1. 根目录下的web.config

Code
      <authentication mode="Forms">
        
<forms loginUrl="Login.aspx" name=".ASPXAUTH"></forms>
      
</authentication><authorization>
        
<deny users="?"/>
      
</authorization>

2.public目录下的web.config,用于放匿名用户也能浏览的页面

Code
<configuration>
    
<system.web>
      
<authorization>
        
<allow users="*"/>
      
</authorization>
    
</system.web>
</configuration>

3.admin目录下的web.config,用于放只有用户名为admin的用户才能浏览

Code
<configuration>
    
<system.web>
      
<authorization>
        
<allow users="Admin"/>
        
<deny users="*"/>
      
</authorization>
    
</system.web>
</configuration>

4. 在login.aspx.cs简单一句验证就可以了

Code
            // 验证
            if (theNode != null)
            {
                
if (theNode.ChildNodes[1].InnerText == TextBox2.Text.Trim())
                {
                    FormsAuthentication.RedirectFromLoginPage(TextBox1.Text.Trim(), 
false);//简单一句
                }
            }
            
            Response.Write(
"<script>alert('没有这个用户名或密码错误!')</script>");

5. 还有一些参考代码

1  登录代码:

a  书本上介绍的

         private void Btn_Login_Click(object sender, System.EventArgs e)

         {

             if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")

              {

                   System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.Txt_UserName.Text,false);

      }

}

b  偶找了 N 久才找到的

private void Btn_Login_Click(object sender, System.EventArgs e)

         {

             if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")

              {

System.Web.Security.FormsAuthentication.SetAuthCookie(this.Txt_UserName.Text,false);

     Response.Redirect("Default.aspx");

     }

}

以上两种都可发放验证后的Cookie,即通过验证,区别:

方法 a) 指验证后返回请求页面,俗称“从哪来就打哪去”。比如:用户没登录前直接在 IE 地址栏输入http://localhost/FormTest/UserInfo.aspx,那么该用户将看到的是Login.aspx?ReturnUrl=UserInfo.aspx,输入用户名与密码登录成功后,系统将根据“ReturnUrl”的值,返回相应的页面

方法 b) 则是分两步走:通过验证后就直接发放Cookie,跳转页面将由程序员自行指定,此方法多用于Default.aspx 使用框架结构的系统。 

2  退出代码:

private void Btn_LogOut_Click(object sender, System.EventArgs e)

     {

System.Web.Security.FormsAuthentication.SignOut();

}

3. 检查是否通过验证

if(User.Identity.IsAuthenticated)

         {

              //你已通过验证,知道该怎么做了吧?

}

User.Identity还有两个属性AuthenticationType(验证类型)与Name(用户名称),大家要注意的是Name属性,此处的User.Identity.Name将得到,验证通过(RedirectFromLoginPageSetAuthCookie)时,我们带入的第一个参数this.Txt_UserName.Text 

Code
private void Submit1_Click (object sender, System.EventArgs e)
{
       
    
if(this.TextBox_username.Text.Trim()== "HR_manager" 
        
&& this.TextBox_password.Text.Trim() == "password")     
    {
         
// Success, create non-persistent authentication cookie.

         FormsAuthentication.SetAuthCookie(
                 
this.TextBox_username.Text.Trim(), flase);
       
         FormsAuthenticationTicket ticket1 
= 
            
new FormsAuthenticationTicket(
                 
1,                                   // version

                 
this.TextBox_username.Text.Trim(),   // get username  from the form

                 DateTime.Now,                        
// issue time is now

                 DateTime.Now.AddMinutes(
10),         // expires in 10 minutes

                 
false,      // cookie is not persistent

                 
"HR"                              // role assignment is stored// in userData

                 );
          HttpCookie cookie1 
= new HttpCookie(
            FormsAuthentication.FormsCookieName, 
            FormsAuthentication.Encrypt(ticket1) );
          Response.Cookies.Add(cookie1);
// 4. Do the redirect. 

          String returnUrl1;
                 
// the login is successful

          
if (Request.QueryString["ReturnUrl"== null)
          {
              returnUrl1 
= "HRpages/HR_main.aspx";
          }
        
          
//login not unsuccessful 

          
else
          {
              returnUrl1 
= Request.QueryString["ReturnUrl"];
          }
          Response.Redirect(returnUrl1);
    }
}