在asp.net中使用javascript进行登录验证 [英] Authentication of login using javascript in asp.net
问题描述
大家好,
我想在asp.net中使用javascript验证我的登录页面。
如果用户经过身份验证,那么它应该重定向到主页,否则应该说无效登录。
我已经管理过这样的东西,但它没有按预期工作。
我的登录控件..
Hello everyone,
I want to authenticate my login page using javascript in asp.net .
if the user is authenticated then it should redirect to home page else it should say invalid login.
I have managed something like this but it is not working as desired.
my login control..
<table style="font-size: 20px;" >
<tr>
<td>
Username
</td>
<td></td>
<td>
<%--<input type="text" runat="server" id="username" name="username" style="height :20px; width :200px;" />--%>
<asp:TextBox ID ="txtUsername" runat ="server" style="height :20px; width :130px;" ></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
Password
</td>
<td></td>
<td>
<%--<input type="password" runat="server" id="password" name="password" style="height :20px; width :200px;"/>--%>
<asp:TextBox ID ="txtPassword" runat ="server" style="height :20px; width :130px;" textmode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<span id="resultspan"></span>
</td>
<td></td>
<td>
<input type="button" name="submit" id="submit" value="Submit" onclick="checkLoginDetails();"/>
</td>
</tr>
</table>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url, cfunc) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.onreadystatechange = cfunc;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
else {
alert("Page can not process");
}
}
function checkLoginDetails() {
// var username = $('#username').val();
// var password = $('#password').val();
var username = document.getElementById('<%= txtUsername.ClientID %>').value;
var password = document.getElementById('<%= txtPassword.ClientID %>').value;
var url = "";
if ((username == null || username == '') && (password == null || password == '')) {
alert("Please enter Username and Password.");
return;
} else if (username == null || username == '') {
alert("Please enter Username.");
return;
} else if (password == null || password == '') {
alert("Please enter Password.");
return;
}
url = "AjaxFunctions.aspx?username=" + username + "&password=" + password + "&Action=login";
loadXMLDoc(url, function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert('Hi');
//document.getElementById("myDiv1").innerHTML = xmlhttp.responseText;
window.location = '<%= ResolveUrl("~/Home.aspx") %>';
}
else {
alert('Invalid login');
}
});
}
</script>
my ajax function...
private void Login()
{
try {
sq = new SQLDataAccessHelper();
string userName = Request.QueryString["username"];
string password = Request.QueryString["password"];
DataSet ds = new DataSet();
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@username", SqlDbType.NVarChar);
param[0].Value = userName;
param[1] = new SqlParameter("@password", SqlDbType.NVarChar);
param[1].Value = password;
ds = sq.ExecuteDataSet("usp_authenticate_user", param);
if ((ds.Tables[0].Rows.Count != 0) && (ds.Tables.Count != 0) && (ds != null) && (ds.Tables[0].Rows[0]["Message"].ToString().ToLower() == "y"))
{
Session["Username"] = userName;
//Response.Write(strXmlval);
//Response.Redirect("Home.aspx", false);
}
else if ((ds.Tables[0].Rows.Count != 0) && (ds.Tables.Count != 0) && (ds != null) && (ds.Tables[0].Rows[0]["Message"].ToString().ToLower() == "n"))
{
// string strXmlval = "<responseText>" + 0 + "</responseText>";
// Response.Write(strXmlval);
}
}
catch (Exception ex) { throw ex; }
finally { }
}
现在什么发生的是........用正确的用户名和密码进入主页...但是对于错误的用户名和密码也会转到主页...
当我使用警报调试javascript时它给了我四倍的警报框作为''嗨'
请帮我如何使这个东西成为可能...
问候,
krunal panchal
now what happens is........with correct userid and password it goes to home page...but for wrong userid and password also it goes to home page...
and when i debugged javascript using alerts it gave me four times the alert box as ''Hi''
please help me how to make this thing possible...
regards,
krunal panchal
推荐答案
('' #username')VAL();
// var password =
('#username').val(); // var password =
('#password')。val();
var username = document.getElementById('<% = txtUsername.ClientID %> ')。值;
var password = document.getElementById('<% = txtPassword.ClientID %> ')。值;
var url =;
if((username == null || username =='')&&(password == null || password =='')){
alert(请输入用户名和密码。);
返回;
} else if(username == null || username ==''){
alert(Please please Username。);
返回;
}否则if(密码== null ||密码==''){
alert(请输入密码。);
返回;
}
url =AjaxFunctions.aspx?username =+ username +& password =+ password +& Action = login;
loadXMLDoc(url,function(){
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
alert( '嗨');
//document.getElementById(\"myDiv1\").innerHTML = xmlhttp.responseText;
window.location ='<% = ResolveUrl( 〜/ Home.aspx)%> ';
}
else {
alert('无效登录');
}
});
}
< / script >
我的ajax函数...
private void登录()
{
try {
sq = new SQLDataAccessHelper();
string userName = Request.QueryString [username];
string password = Request.QueryString [password];
DataSet ds = new DataSet();
SqlParameter [] param = new SqlParameter [2];
param [0] = new SqlParameter(@ username,SqlDbType.NVarChar);
param [0] .Value = userName;
param [1] = new SqlParameter(@ password,SqlDbType.NVarChar);
param [1] .Value =密码;
ds = sq.ExecuteDataSet(usp_authenticate_user,param);
if((ds.Tables [0] .Rows.Count!= 0)&&(ds.Tables.Count!= 0)&&(ds!= null)&&( ds.Tables [0] .Rows [0] [Message]。ToString()。ToLower()==y))
{
Session [Username] = userName;
//Response.Write(strXmlval);
//Response.Redirect(\"Home.aspx,false);
}
else if((ds.Tables [0] .Rows.Count!= 0)&&(ds.Tables.Count!= 0)&&(ds!= null )&&(ds.Tables [0] .Rows [0] [Message]。ToString()。ToLower()==n))
{
// string strXmlval =< responseText > + 0 +< / responseText > 跨度>;
// Response.Write(strXmlval);
}
}
catch(Exception ex){throw ex; }
finally {}
}
('#password').val(); var username = document.getElementById('<%= txtUsername.ClientID %>').value; var password = document.getElementById('<%= txtPassword.ClientID %>').value; var url = ""; if ((username == null || username == '') && (password == null || password == '')) { alert("Please enter Username and Password."); return; } else if (username == null || username == '') { alert("Please enter Username."); return; } else if (password == null || password == '') { alert("Please enter Password."); return; } url = "AjaxFunctions.aspx?username=" + username + "&password=" + password + "&Action=login"; loadXMLDoc(url, function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { alert('Hi'); //document.getElementById("myDiv1").innerHTML = xmlhttp.responseText; window.location = '<%= ResolveUrl("~/Home.aspx") %>'; } else { alert('Invalid login'); } }); } </script> my ajax function... private void Login() { try { sq = new SQLDataAccessHelper(); string userName = Request.QueryString["username"]; string password = Request.QueryString["password"]; DataSet ds = new DataSet(); SqlParameter[] param = new SqlParameter[2]; param[0] = new SqlParameter("@username", SqlDbType.NVarChar); param[0].Value = userName; param[1] = new SqlParameter("@password", SqlDbType.NVarChar); param[1].Value = password; ds = sq.ExecuteDataSet("usp_authenticate_user", param); if ((ds.Tables[0].Rows.Count != 0) && (ds.Tables.Count != 0) && (ds != null) && (ds.Tables[0].Rows[0]["Message"].ToString().ToLower() == "y")) { Session["Username"] = userName; //Response.Write(strXmlval); //Response.Redirect("Home.aspx", false); } else if ((ds.Tables[0].Rows.Count != 0) && (ds.Tables.Count != 0) && (ds != null) && (ds.Tables[0].Rows[0]["Message"].ToString().ToLower() == "n")) { // string strXmlval = "<responseText>" + 0 + "</responseText>"; // Response.Write(strXmlval); } } catch (Exception ex) { throw ex; } finally { } }
现在什么发生的是........用正确的用户名和密码进入主页...但是对于错误的用户名和密码也会转到主页...
当我使用警报调试javascript时它给了我四倍的警报框作为''嗨'
请帮我如何使这个东西成为可能...
问候,
krunal panchal
now what happens is........with correct userid and password it goes to home page...but for wrong userid and password also it goes to home page...
and when i debugged javascript using alerts it gave me four times the alert box as ''Hi''
please help me how to make this thing possible...
regards,
krunal panchal
你的代码还可以,它''只是不完整。
因为在你的回调函数中:
Your code is OK, it''s just incomplete.
Because in your callback function:
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
alert(''嗨'');
//document.getElementById(\"myDiv1\").innerHTML = xmlhttp.respons eText;
window.location =''<%= ResolveUrl(〜/ Home.aspx)%>'';
}
else {
alert(''登录无效');
}
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(''Hi'');
//document.getElementById("myDiv1").innerHTML = xmlhttp.responseText;
window.location = ''<%= ResolveUrl("~/Home.aspx") %>'';
}
else {
alert(''Invalid login'');
}
你没有处理回应。你只需更改window.location,无论用户/通行证是否正确。
在您的C#代码中,响应被引用为注释:
You don''t handle the response. You just change the window.location no matter if the user/pass were correct or not.
in your C# code the response is quoted as remark:
// string strXmlval =< responsetext> + 0 +;
// Response.Write(strXmlval);
// string strXmlval = "<responsetext>" + 0 + "";
// Response.Write(strXmlval);
干杯,
Edo
Cheers,
Edo
这篇关于在asp.net中使用javascript进行登录验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!