ASP.NET使用Login控制項與連結自己的資料庫

  • 6665
  • 0
  • 2015-02-02

摘要:ASP.NET登入頁面與網站各網頁權限卡控

Login控制項,登入時用的控制項,

Loing控制項是用Forms authentication的方式去進行使用者驗證使用FormsAuthentication Class類別

可以參考 :

Msdn : https://msdn.microsoft.com/en-us/library/aa480476.aspx

Blog : http://blog.miniasp.com/post/2008/02/21/Explain-Forms-Authentication-in-ASPNET-20.aspx

 

 

如何一步一步新增Login控制項可以參考: http://www.dotblogs.com.tw/topcat/archive/2008/03/05/1237.aspx

1.需要在web.config註冊要使用的Page

<system.web>

<forms loginUrl="Login.aspx" />

    </authentication>

  </system.web>

2.新增Login.aspx Page,新增Login控制項

//要取得控制項中的Username and Password可以透過FindControl()

Login Login1 = (Login)LoginView1.FindControl("Login1");

string user = Login1.UserName.Trim();

string pssword = Login1.Password.Trim();

 

3.Login送出登入後會觸發Authenticate()事件,我們需要實作若登入時進行的程式,並寫入chkLogin(user , Password)從自己的資料庫中判斷

Using System.Web.Security

protected void Login_Authenticate(object sender, AuthenticateEventArgs e) {

  {

       Login Login1 = (Login)LoginView1.FindControl("Login1");

       string user = Login1.UserName.Trim();

       string pssword = Login1.Password.Trim();

        //未經過驗證

        if (e.Authenticated == false)

        {

            if (chkLogin(user, pssword))

            {

                //取得或設定在登入嘗試成功時顯示給使用者之頁面的 URL。

                Login1.DestinationPageUrl = "~/HRMS/Default.aspx";

                //將已驗證的使用者重新導向回到原來要求的 URL 或預設 URL。

                FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);

                //已經過驗證

                e.Authenticated = true;

                Session["UserId"] = "user";

                Session["IsLogin"] = "OK";

            }

            else

            {

                e.Authenticated = false;

                Session["UserId"] = "";

                Session["IsLogin"] = "NG";

                //將瀏覽器重新導向至包含指定查詢字串的登入 URL。

                //FormsAuthentication.RedirectToLoginPage(Login1.UserName);

            }

        }

}

 private Boolean chkLogin(string username, string password)

    {

        string strConn = ConfigurationManager.ConnectionStrings["FAB51HRMS"].ConnectionString;

        SqlConnection sqlConn = new SqlConnection(strConn);

        String strSQL = "select EMPLOYEE_NO, AUTHORITY from zHrms_WD_EMPLOYEE_DETAIL where EMPLOYEE_NO =" + username + "and PASSWD =" + password;

        SqlDataReader dr = null;

        SqlCommand command = new SqlCommand(strSQL, sqlConn);

        try

        {

            sqlConn.Open();

            dr = command.ExecuteReader();

            if (dr.HasRows)

            {            

               return true;

            }

            else

            {

               return false;

            }

        }//End Try

        catch

        {

            return false;

        }//End Catch

        finally

        {

            if (dr != null)

            {

                command.Cancel();

                dr.Close();

            }

            if (sqlConn.State == ConnectionState.Open)

            {

                sqlConn.Close();

            }

        }//End finally

    }//End chkLogin

4.用Session與用FromAuthenticate的差別,

Session : 只能有預設20 min(但也是可以修改預設時間,不閒置可以用超過20分鐘),Session的好處是綁定單一瀏覽器(但關閉網頁就會不見)

<system.web>
  <sessionState mode="StateServer"
    stateConnectionString="tcpip=SampleStateServer:42424"
    cookieless="false"
    timeout="20"/>
</system.web>

FromAuthenticate : 紀錄30分鐘


<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>
</system.web>