關於ASP.NET中的Forms身分識別驗證詳解

來源:互聯網
上載者:User

      每個web開發人員,都或多或少的接觸到了身分識別驗證,而接觸最多的大概就是windows驗證和Forms身分識別驗證了,本文這裡就詳解下Forms身分識別驗證,對過去一段時間的學習做一個總結。
       在開始之前,我將建立一個web項目用來測試,下面是幾個主要的目錄:
        Admin目錄: 管理員目錄 --Default.aspx 管理員目錄下的頁面 User目錄: 普通使用者目錄 --Default.aspx 普通使用者目錄下的頁面 Root目錄: 根目錄 --Default.axpx 根目錄下的預設頁面 --Login.aspx 根目錄下面的登入頁面 --web.config 根目錄下的設定檔 --Global.asax 根目錄下的全域控制檔案(在VS2005後需要手工添加)  
       後續我們將針對web.config的配置,Login頁面驗證身份和Global.asax中對使用者身份的驗證來描述一個完整的驗證過程。
         一:web.config設定 <!--啟用Forms身分識別驗證--> <authentication mode="Forms"> <forms name="BeginningWeb2008.APSXAUTH" loginUrl="login.aspx" path="/" protection="All" /> </authentication> <!--拒絕所有匿名使用者存取--> <authorization> <deny users="?"/> <allow users="*"/> </authorization> <!--設定Login.aspx可以匿名訪問--> <location path="default.aspx"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location> 

        //二:身分識別驗證 protected void Button1_Click(object sender, EventArgs e) { if (txtUserName.Text != "" && txtPassword.Text != "") { string user_name = txtUserName.Text; FormsAuthentication.Initialize(); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user_name, DateTime.Now, DateTime.Now.AddMinutes(5), true, "user defined data here", FormsAuthentication.FormsCookiePath); string encTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(cookie); Response.Redirect(FormsAuthentication.GetRedirectUrl(user_name, true)); } } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.