如何使用asp.net在Jquery中validation用户名密码?

我有使用这个进入asp.net的Jquery登录控件loke的代码,但是我很困惑在哪里以及如何简单地validation用户名和密码。

Login.aspx

      
Forgot your password?
Login.js // Login Form $(function() { var button = $('#loginButton'); var box = $('#loginBox'); var form = $('#loginForm'); button.removeAttr('href'); button.mouseup(function(login) { box.toggle(); button.toggleClass('active'); }); form.mouseup(function() { return false; }); $(this).mouseup(function(login) { if(!($(login.target).parent('#loginButton').length > 0)) { button.removeClass('active'); box.hide(); } }); });

现在我怎么能用这个Jquery代码使用我的asp.net身份validation? 在哪里以及如何编写c#身份validation代码? 在jquery中给一些新的bie感谢

我不确定你要做什么,但为了用asp.net执行登录,你应该以某种方式将数据发送到服务器。 您可以通过两种方式完成此操作:使用表单或AJAX请求发送它。 如果您想在表单中发送它,您应该在标签表单中指定操作,例如

 

如果你想通过jQuery AJAX请求发送它,你可以使用jQuery.ajax方法,这里有明确的解释, url参数中有相应的url。
在服务器端,您的身份validation可能如下所示:

 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( // create authentication ticket 1, // id email, // user name DateTime.Now, // date of issue DateTime.Now.AddMinutes(15), // expiration date true, // true if your ticket will be stored in a cookie, that will be kept by browser across the sessions userdata, // any text data, that you want to add to ticket (eg user roles) FormsAuthentication.FormsCookiePath // path associated with your ticket ); string hashedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, hashedTicket); // adding your ticket to cookie cookie.Expires = ticket.Expiration; Response.Cookies.Add(cookie); // sending cookie to client with current response 

执行这些操作后,您的客户端将被识别为已通过身份validation,直到他注销或Cookie /票证到期时间到期为止。
希望这些信息对您有所帮助。

以下是编写ajax调用的方法

 var usernameText = $("#usernameTextbox"); var passwordText = $("#passwordTextbox"); $.ajax({ type: 'GET', url: '/Account/ValidateUser', async: true, dataType: 'json', data: { userName: usernameText, password: passwordText }, success: function(result) { if(result){ // redirect user to required page } else{ // show error on some div that usrname and password do not match } } }); 

关于SO的相关问题

MVC 4 Jquery Ajax调用返回登录页面html

如何在jQuery和AJAX中使用Basic Auth?