2015-12-13 26 views
1

這裏是我的C#代碼檢查用戶名或密碼真或使用jQuery的web服務假

public void Login(string kullaniciAd, string sifre) 
    { 
     try 
     { 
      SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress; Initial Catalog=twitter;Integrated Security=SSPI"); 
      if (conn.State == ConnectionState.Closed) 
       conn.Open(); 

      SqlCommand cmd = new SqlCommand("SELECT * FROM tblKullanici WHERE kullaniciAdi = @kullaniciAd or sifre = @kullaniciSifre", conn); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = conn; 
      cmd.Parameters.AddWithValue("@kullaniciAd", kullaniciAd);//username 
      cmd.Parameters.AddWithValue("@kullaniciSifre", sifre); //password 
      cmd.ExecuteNonQuery(); 
      int UserExist = (int)cmd.ExecuteScalar(); 
      if (UserExist > 1) 
      { 
       //if given there is a user with given username and password?? 
       // i don't know how to do that first should i check username and then check that username's password or what?? 
       //successful 
      } 
      else 
      { 
       //wrong password or username 
      } 
      conn.Close(); 
     } 
     catch (SoapException se) 
     { 
      throw se; 
     } 
    } 

,這裏是我的aspx頁面

<script> 
    $(document).ready(function() { 
     $('#btnLogin').click(login); 
    }); 
    function login() { 
     var kullaniciAd = $('#userName').val(); 
     var sifre = $('#password').val(); 
     $.ajax({ 
      url: "http://localhost:63000/LoginService.asmx/Login", 
      type: "POST", //or should it be GET? 
      dataType: "json", 
      data: JSON.stringify({ kullaniciAd: kullaniciAd, sifre: sifre }), 
      contentType: "application/json; charset=windows-1254", 
      success: function() { 
       alert("");// i have an alert div down there how can i show if login is successful or not in that alert div 
      }, 
      error: function (e) { 
       alert(e.statusText); 
      } 
     }) 
    } 
</script> 
<%--<div class="alert alert-danger alert-dismissable"> 
     <button contenteditable="false" type="button" class="close" data-dismiss="alert" 
       aria-hidden="true"> 
       ×</button> 
       <strong>Well done!</strong>You successfully read this important alert message. 
</div>--%> 

它似乎很容易,但我不能決定如果我檢查真實或錯誤的Web服務,只發送jquery只有一個數據(true,false)或檢查是否true或false在jquery中調用true或false web方法?

回答

0

這是我第一次看到登錄邏輯,檢查是否有任何用戶匹配輸入的密碼。我的建議..不。

更好的方法是檢查具有給定用戶標識的用戶是否存在,然後從數據存儲中返回存儲的密碼(和鹽)。接下來的步驟是散列輸入的普通密碼,並用儲值。這給你三種可能的結果

  • 用戶是已知的,但輸入的密碼不正確,
  • 用戶未找到(SQL查詢返回0結果)
  • 用戶發現和密碼是正確的

您需要做的唯一選擇是您是否想要報告前兩種情況之間的差異。有人會認爲這是一個安全問題,因爲它可以用於phising ..你可以找出你的系統上註冊了什麼用戶,所以很多系統會簡單地報告「用戶/密碼組合不正確」

有關更多信息使用哈希來保護您的密碼存儲 https://crackstation.net/hashing-security.htm

相關問題