2011-08-22 64 views
0

我有一個數據庫驅動的菜單,通過它我可以看到特定角色可以查看的頁面。但是,如果用戶鍵入網址,他仍然可以查看該網頁....你能告訴我怎麼能阻止他這麼做嗎?然而,我試圖檢查角色是否通過查詢訪問頁面,然後將他重定向到另一個,如果他沒有..那麼,你能告訴我最好的方法,我可以做這個任務..asp.net中的安全性

這是我做過什麼

public bool Initi() 
{ 

    string currentuser = HttpContext.Current.User.Identity.Name; 
    string currentPageName = HttpContext.Current.Request.Url.AbsoluteUri; 
    string connStr1 = "Data Source=NISHANTH-PC\\SQLEXPRESS;Initial Catalog=roletesting;Integrated Security=True"; 
    using (SqlConnection conn1 = new SqlConnection(connStr1)) 
    { 
     conn1.Open(); 
     SqlParameter param1 = new SqlParameter(); 

     param1.ParameterName = "@currentpagename"; 

     param1.SqlDbType = SqlDbType.NVarChar; 

     param1.Direction = ParameterDirection.Input; 

     param1.Value = currentPageName; 

     SqlParameter param = new SqlParameter(); 

     param.ParameterName = "@currentuser"; 

     param.SqlDbType = SqlDbType.NVarChar; 

     param.Direction = ParameterDirection.Input; 

     param.Value = currentuser; 
     string hasaccess = "select PageRole.hasRights from PageRole, 
      aspnet_UsersInRoles, SubMenu,aspnet_Paths,aspnet_Roles,aspnet_Users where 
      [email protected] and Submenu.Url = aspnet_Paths.Path and 
      aspnet_Paths.PathId=PageRole.PathId and PageRole.RoleId = '780c6d23-b321- 
      43fc-98fe-d2af26b6f069' "; 

     SqlCommand coi = new SqlCommand(hasaccess, conn1); 
     coi.Parameters.Add(param1); 
     coi.Parameters.Add(param); 
     string a = (string)coi.ExecuteScalar(); 
     if (a == "null" || a == "N") 
     { 
      return false; 
     } 
     else 
      return true; 
    } 

我只是硬編碼中直接使用角色ID值.....然後在主我只需相應檢查,如果這不是真的去做。但是,儘管我在母版頁上做了這件事,但對我來說這似乎更糟糕。由於我對asp.net相當陌生,我不知道什麼是最好的方式來混合。所以,你可以讓我知道最好的方法,也是我在這裏犯的錯誤...

回答