如何使用Web.config的<authentication>节实现Form认证

练习要求:-------------如何使用Web.config的<authentication>节实现Form认证:无论用户登录网站的哪一个页面都会导航到登录页面。该示例包含两个页面,一个是Default.aspx页面该页面为登录页面,当用户输入正确的用户名和密码后,导航到欢迎页面。另一个是Welcome.aspx页面,该页面显示登录欢迎信息。

搞了一天的垃圾问题!不过按照书上的示例还是没能搞定,好多问题。下为可行方案

Welcome.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Welcome to the World of the  ASP.NET " + Request.QueryString["id"]);
    }
}
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "1" && TextBox2.Text == "1")
        {
            //System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text, false); 

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

            Response.Redirect("Welcome.aspx?id=" + this.TextBox1.Text ); 

        }
    }
}
Web.config
<?xml version="1.0"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
      <system.web>

        <authentication mode="Forms">
          <forms loginUrl="Default.aspx" name="FormsAuth"></forms>
        </authentication>
        <authorization>
          <deny users="?"/>
        </authorization>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>

现把书中的的示例程序附在下面,望看到的高手予以解决啊。(见http://zhidao.baidu.com/question/505547961.html?quesup2)

 

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"); 



下面的代码示例演示如何允许匿名用户获得 Logon.aspx 页的访问权。 

<configuration>
   <location path="Logon.aspx"><!--location应该在<configuration><system.web>之间,而不应该包含到<system.web>..</system.web>之间;-->
      <system.web>
         <authorization>
            <allow users="?"/>
         </authorization>
      </system.web>
   </location>
   <system.web>........
</configuration>

下面的代码示例演示如何仅将指定页的上载文件大小限制设置为 128 KB。 

<configuration>
   <location path="UploadPage.aspx">
      <httpRuntime maxRequestLength="128"/>
   </location>
</configuration>

下面的代码示例演示如何防止配置设置被子目录中的 Web.config 文件更改。 

<configuration>
   <location allowOverride="false"/>
</configuration>
 

判断验证与否及获取验证后的用户信息

有的时候,在同一张页面需要判断用户是否已经登录,然后再呈现不同的布局。有人喜欢用 Session 来判断,我不反对此类做法,在此我只是想告诉大家还有一种方法,且看下面代码: 


if(User.Identity.IsAuthenticated) 


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



User.Identity 还有两个属性AuthenticationType(验证类型)与 Name(用户名称) ,大家要注意的是 Name 属性,此处的User.Identity.Name将得到,验证通过(RedirectFromLoginPage 或SetAuthCookie)时,我们带入的第一个参数 this.Txt_UserName.Text 。这个参数很重要,关系到种种……种种的情况,

<?xml version="1.0" encoding="utf-8"?> 

<configuration> 

<system.web> 

<authorization> 

<allow users="Admin"/> 

<deny users="*"/> 

</authorization> 

</system.web> 

</configuration>

 

System.Web.Security.FormsAuthentication.SetAuthCookie(this.Txt_UserName.Text,false); //通过验证,发放 Cookie 

之前我曾强调,要注意,第一个参数很重要,重要到什么程度?说到这,恐怕地球人都知道了——它就是allow与deny的依据。假如此处用户填写的是“Admin”即 this.Txt_UserName.Text = "Admin"; 那么进入系统后,他就能访问你设定好的目录下的网页了,其它闲杂人等一律拒之门外

posted @ 2012-12-08 13:38  .NET~莫愁  阅读(309)  评论(0编辑  收藏  举报