2005-12 月份文章 顯示方式:簡文 | 列表

December 5,2005

[ASP.Net]使用FORM Authentication和URL Authorization撰寫登入系統

用到Forms Authentication 和URL Authorization 配合 Sql Server設定帳號所屬群組的陽春登入系統。

登入系統檔名:Login_K.aspx
命名空間:using System.Web.Security;   using System.Data.SqlClient;
Submit button:配合OnClick事件觸發CheckPassword Function

CodeBehind:

protected void CheckPassword(object obj, EventArgs e )
  {
       string OriginalUrl;
       string roles=null;
       bool isAuthenticated = IsAuthenticated(txtAccount.Text,txtPwd.Text, ref roles);  //驗證帳號密碼是否符合
       if(isAuthenticated == true)
       {   
            SetupCookie(txtAccount.Text, roles);
            FormsAuthentication.SetAuthCookie(roles.ToString(), false);      //以cookie方式儲存所屬群組資訊
            OriginalUrl = FormsAuthentication.GetRedirectUrl(roles.ToString(),false);  //取得原來要瀏覽的網頁網址
             FormsAuthentication.RedirectFromLoginPage(roles.ToString(),false);    //驗證完後要瀏覽的網址, false 代表關閉borwsre後cookie是否保留 
      }
     else
     {
         FormsAuthentication.SignOut();
      }


private bool IsAuthenticated(string username, string password,ref string roles)   //驗證帳號密碼是否符合
{
      SqlConnection conn = new SqlConnection(Config.CONN_STRING);
      conn.Open();
      string sql ="select * from ACCOUNT_MNG where ACCOUNT='"+username+"' and PWD='"+password+"'";
      SqlCommand cmd = new SqlCommand(sql,conn);
      SqlDataReader dr = cmd.ExecuteReader();
      if(dr.Read())
      {
           roles = dr["ROLE"].ToString(); 
           return true; 
       }
      else
      {
            return false;
      }
}


void SetupCookie(string account,string group)
{
     SqlConnection conn = new SqlConnection(Config.CONN_STRING);
     conn.Open();
     string sql=null;
     // 由角色(群組)決定查詢哪個資料表格
     if(group == "admin")
            sql="select * from NBIC_STUFF where EMP_NO='"+account+"'";
    else if(group == "enterprise")
            sql="select * from EMPLOYEEE  where EMP_ID='"+account+"'";
    SqlCommand cmd = new SqlCommand(sql,conn);
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();

     if(group == "admin")
              Response.Cookies["ACCOUNT"]["EMP_NO"]= dr["EMP_NO"].ToString();
   else if(group == "enterprise")
              Response.Cookies["ACCOUNT"]["EMP_NO"]= dr["EMP_ID"].ToString();

   Response.Cookies["ACCOUNT"]["EMP_NAME"]=dr["EMP_NAME"].ToString();
   Response.Cookies["ACCOUNT"]["JOB_TITLE"]=dr["JOB_TITLE"].ToString();
   Response.Cookies["ACCOUNT"]["PHONE"]=dr["PHONE"].ToString();
   Response.Cookies["ACCOUNT"]["MOBILE_PHONE"]=dr["MOBILE_PHONE"].ToString();
   Response.Cookies["ACCOUNT"]["FAX"]=dr["FAX"].ToString();
   Response.Cookies["ACCOUNT"]["EMAIL"]=dr["EMAIL"].ToString();
   Response.Cookies["ACCOUNT"].Expires= DateTime.Now.AddMinutes(60);
}


Web.config設定
<configuration>
  <system.web>
    <authentication mode="Forms">               //表單驗證 Form authentication
          <forms loginUrl="Login_K.aspx"   name="ckName">     //loginUrl未登入的帳號指定跳到Login_K.aspx
         </forms>
    </authentication>
 </system.web>

<location path ="" allowOverride="true" >     //location用來描述虛擬路徑URL
   <system.web>
       <authorization>                                     //配合location使用Url Authorization
             <allow users="admin"   />
             <deny users ="?,enterprise" />
       </authorization>
   </system.web>
</location>

<location path ="enterprise" allowOverride="true" >
    <system.web>
        <authorization>
            <allow users="enterprise,admin"   />
            <deny users ="?" />
        </authorization>
     </system.web>
</location>

</configuration>


之後的系統畫面如果要用到cookie設定登入者的資訊可直接利用以下程式碼
   txtSend.Value= Request.Cookies["ACCOUNT"]["EMP_NAME"];
ACCOUNT 是 cookie 名稱
EMP_NO 是資料庫表格中的欄位名稱
其它欄位對照資料庫中名稱,例:
   txtEmail.Value= Request.Cookies["ACCOUNT"]["EMAIL"];


Posted by soboring at 10:16回應(2)引用(0)網際網絡

December 2,2005

[ASP.Net]CodeBehind web control button結合前瑞javascript的方法

Codebehind的情形之下,利用web form控制項(HTML控制項以伺服器端執行不能run)在中的Attribute屬性,可以和前端的javascript做結合,當我們新增一個Attribute屬性,就代表著最後產生HTML code之後的一個屬性或方法。
常常寫程式會遇到double check的情形以增加程序上的正確性,可利用如下程式碼。

private void Page_Load(object sender, System.EventArgs e)
{
    btnSubmit.Attributes.Add("onclick","javascript:return confirm('sure?');");
}


Posted by soboring at 18:17回應(0)引用(0)筆記備忘
 [第一頁]  [1]  [2]