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

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 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);
    }
}