如何使用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"; 那么進入系統后,他就能訪問你設定好的目錄下的網頁了,其它閑雜人等一律拒之門外
轉載于:https://www.cnblogs.com/fighting-mochou/archive/2012/12/08/2808510.html
總結
以上是生活随笔為你收集整理的如何使用Web.config的authentication节实现Form认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET 如何catch存储过程中
- 下一篇: poj 1390(消除方块(blocks