熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

一個簡單的ASP.NET Forms 身份認證

2013-11-13 09:52:40  來源: .NET編程 

  當訪問默認首頁defaultaspx時會自動跳轉到loginaspx頁面上請求登錄隨便輸入用戶名和密碼點擊登錄按鈕會回到首頁並顯示當前登錄的用戶名

  nfig <configuration> <systemweb> <compilation debug=true/> <authentication mode=Forms> <forms loginUrl=loginaspx name = ASPXFORMSAUTH /> </authentication> <authorization> <deny users=/> </authorization> </systemweb> </configuration>

  defaultaspx <HTML> <HEAD> <TITLE>首頁</TITLE> <script language=VB runat=server> Sub Page_Load(Sender As Object e As EventArgs)

  MessageText = StringFormat(你好{} ContextUserIdentityName)

  End Sub sub btnSignout_Click(Sender as Object E as EventArgs)

  FormsAuthenticationSignOut()

  ResponseRedirect(loginaspx

  end sub

  </script> </HEAD> <BODY> <asplabel id=Message runat=server/> <br> <form method=post runat=server> <aspbutton id=btnSignout Text=退出登錄 runat=server OnClick=btnSignout_Click/> </form> </BODY> </HTML>

  loginaspx <HEAD> <TITLE>首頁</TITLE> <script lanugage=c# runat=server> private void Page_Load(object sender EventArgs e)

  { if (RequestIsAuthenticated)

  ResponseRedirect(defaultaspx} void btnLogin_Click(Object sender EventArgs e)

  { if (txtUsernameText != null && txtUsernameText != StringEmpty && txtPasswordText != null && txtPasswordText != StringEmpty)

  FormsAuthenticationRedirectFromLoginPage(txtUsernameText true)else lblErrorText = 錯誤的用戶名/密碼} </script> </HEAD> <BODY> <form method=post runat=server>

  <aspLabel id=lblUsername runat=server Text=用戶名/>

  <aspTextbox id=txtUsername runat=server/> <br> <aspLabel id=lblPassword runat=server Text=密 碼/>

  <aspTextbox id=txtPassword runat=server TextMode=password/> <br> <aspbutton id=btnLogin runat=server Text=登錄 OnClick=btnLogin_Click />

  </form> <hr> <br> <aspLabel id=lblError forecolor=red runat=server/> </BODY> </HTML>


From:http://tw.wingwit.com/Article/program/net/201311/11835.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.