login控件的主要代碼







登錄頁面的前臺代碼
<asp:Login ID="Login1" runat="server" onauthenticate="Login1_Authenticate" DestinationPageUrl="~/WebPage/Admin.aspx">
</asp:Login>
        
        
登錄頁面的後臺代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using LoginControlTest1.Entities;




namespace LoginControlTest1.WebPage
{
    public partial class SignIn : System.Web.UI.Page
    {




        //page load
        protected void Page_Load(object sender, EventArgs e)
        {


        }




        //檢查用戶是否存在(測試)
        protected bool UserExist(string account, string password)
        {
            if (account == "1" && password == "1")
            {
                return true;
            }
            return false;
        }




        //得到用戶信息(測試)
        protected UserModel GetUserInfo(string account, string password)
        {
            UserModel user = null;
            if (account == "1" && password == "1")
            {
                user = new UserModel
                {
                    Name = account,
                    Role = "測試"
                };
            }
            return user;
        }






        //登錄事件
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string account = ((Login)sender).UserName;
            string pwd = ((Login)sender).Password;
            if (GetUserInfo(account, pwd) != null)
            {
                e.Authenticated = true;
                Session["User"] = GetUserInfo(account, pwd);
            }
            else
            {
                e.Authenticated = false;
            }
        }






    }//end   class


}








管理員頁面的後臺代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using LoginControlTest1.Entities;
using System.Web.Security;


namespace LoginControlTest1.WebPage
{
    public partial class Admin : System.Web.UI.Page
    {






        //page  load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                this.Label1.Text = "歡迎:" + ((UserModel)Session["User"]).Name;
            }
        }






        //註銷按鈕
        protected void Button1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("Home.aspx");
        }








    }//end   class
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章