2015-06-05 27 views
-1

這是我的控制器,我創建了一個DataTable。使用sql數據庫表屬性作爲日誌用於Web應用程序中的用戶憑據

public class HomeController : Controller 
    { 
     string connString = "Data source=DEVELOPER1; Initial catalog=Temp;Integrated security=True"; 
     string commString = ""; 

     public ActionResult Index() 
     { 
      List<string> list = new List<string>(); 
      commString = "select * from usermaster"; 
      DataTable datatable = GetDataTable(commString); 
      return View(datatable); 
     } 
     public DataTable GetDataTable(string strQuery) 
     { 
      DataTable datatable = new DataTable(); 
      using (SqlConnection conn = new SqlConnection(connString)) 
      { 
       conn.Open(); 
      using (SqlCommand comm = new SqlCommand(commString, conn)) 
      { 
        SqlDataReader sqlDataReader = comm.ExecuteReader(); 
        datatable.Load(sqlDataReader); 
       } 
      } 
      return datatable; 
     } 

這是我的看法。

<input type="text" id="text1" value="" /> 
<br /> 
<br /> 
<input type="text" id="text2" value="" /> 
<br /> 
<br /> 
<input type="button" id="button1" value="Button" onclick="button1_click()" /> 

下面是我在commString已經使用了查詢的截圖。

enter image description here 這是我當前項目位置輸出的圖像。

enter image description here

現在,我的要求是,如果在這兩個文本字段中把「ADM」的用戶,然後它會顯示警告框「真」,否則將顯示「假」。 即使用戶留下任何一個空白,也會顯示「false」。 請幫忙。

+0

讓我們看看'Download' [發佈]行動 –

+0

你傳遞整個用戶名+密碼列表中視圖(即使只有一個值),並且你已經標記了這個jQuery(儘管除了ajax帖子之外沒有javascript)。您是否在尋找一些javascript來檢查用戶名和密碼並顯示警告/消息框?不要 - 這將會是一個巨大的安全風險,並且無法登錄。 –

+0

這只是一個原型,我在學術水平。我只需要向我的教員展示完成的任務。 – Amit

回答

1

看起來像這樣的事情可能是你以後

  • 沒有通過允許登錄+密碼的觀點,任何人都可以做瀏覽器的查看源代碼,看看有效登錄
  • 相反,通過輸入的用戶+傳遞到所述控制器,並檢查有
  • 使用POST,因爲它是難以截獲
  • 考慮使用現有的,內置登錄機制如表格登錄

客戶端代碼:

function button1_click() { 
    $.ajax({ 
     url: "/Home/Login", 
     method: "POST", 
     data: { 
      name: $('#text1').val(), 
      password: $('#text2').val(), 
     }, 
     dataType: "json", 
     success: function (data) { 
      alert(data); 
     } 
    }); 

控制器:

public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Login(string name, string password) 
    { 
     commString = "select * from usermaster"; 
     DataTable datatable = GetDataTable(commString); 

     // Check datatable contains name + password here 
     bool found = ... 

     return new JsonResult() { Data = found }; 
    } 
+0

非常感謝。你的解決方案奏效 – Amit

1

我在這裏只是檢查字符串爲 「ADM」 不是從數據庫

在查看你需要寫

function button1_click() { 
 
var url = '@Url.Action("Download","Home")'; 
 
    $.ajax({ 
 
     url: url, 
 
     method: "POST", 
 
     data: { 
 
      name: $('#text1').val(), 
 
      password: $('#text2').val(), 
 
     }, 
 
     dataType: "json", 
 
     success: function (data) { 
 
      alert(data); 
 
     } 
 
    });

並在控制器

public ActionResult Download(string text1, string text2) 
 
     { 
 
      if (text1 == null || text2 == null) 
 
      { 
 
       return Json(false, JsonRequestBehavior.AllowGet); 
 
      } 
 
      else 
 
      { 
 
       if (text1 == "adm" && text2 == "adm") 
 
       { 
 
        return Json(true, JsonRequestBehavior.AllowGet); 
 
       } 
 
       else 
 
       { 
 
        return Json(false, JsonRequestBehavior.AllowGet); 
 
       } 
 
      } 
 
     }

相關問題