使用web服务使用jquery检查用户名或密码是真是假

本文关键字:密码 用户 检查 服务 web jquery 使用 | 更新日期: 2023-09-27 18:26:32

这里是我的C#代码

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服务中检查true还是false,只发送jquery一个数据(true,false),还是在jquery中通过调用true还是false的web方法来检查true或false??

使用web服务使用jquery检查用户名或密码是真是假

这是我第一次看到登录逻辑,它检查是否有与输入的密码匹配的用户。我的建议。。不要。

更好的方法是检查具有给定userid的用户是否存在,然后从数据存储中返回存储的密码(和salt)。下一步是对输入的普通密码进行散列,并使用存储的值进行检查。这给了你三种可能的结果

  • 用户已知,但输入的密码不正确
  • 找不到用户(sql查询返回0个结果)
  • 用户已找到,密码正确

您需要做出的唯一选择是是否要报告前两种情况之间的差异。有些人会认为这是一个安全问题,因为它可能被用于钓鱼。。你可以在你的系统中找到注册的用户,所以很多系统只会报告"用户/密码组合不正确"

有关使用哈希保护密码存储的更多信息https://crackstation.net/hashing-security.htm