首页 > 代码库 > 关于asp:login控件和验证码的问题?(转)
关于asp:login控件和验证码的问题?(转)
1.验证码页面添加.
2.将这验证码页面添加到login控件中:
拖曳一Login控件,将之切换到模式下,在Html源文件中在表格中密码那行后添加:
<tr>
<td style="width: 151px">
<asp:Label ID="Label1" runat="server" AssociatedControlID="Password">验证码:</asp:Label>
</td>
</tr>
<tr>
<td style="width: 151px">
<asp:TextBox ID="txtImgValid" runat="server" Font-Size="0.8em" ></asp:TextBox>
<asp:ImageButton id="imgValid" ImageUrl="~/public/imageValidate.aspx" runat="server" />
</td>
</tr>
其中ImageUrl="~/public/imageValidate.aspx“ 的imageValidate.aspx指的就是我们第一步建立的验证码文件。
3.在login控件的authenticate事件中加入代码
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
TextBox txtImage = Login1.FindControl("txtImgValid") as TextBox;
if (Session["CheckCode"] == null)
{
Response.Write("<script language=‘javascript‘>alert(‘未可知错误!‘)</script>");
e.Authenticated = false;
return;
}
if (String.Compare(Session["CheckCode"] .ToString(),txtImage.Text,true)!=0)
{
Response.Write("<script language=‘javascript‘>alert(‘不匹配!‘)</script>");
e.Authenticated = false;
return;
}
e.Authenticated = true;
}
这样的做法只能验证验证码,而用户身份根本没有验证。
即使输入错误的用户和密码,只要验证码正确,就能登陆成功。
这是为什么,该怎么解决?
原文:http://social.msdn.microsoft.com/Forums/ie/zh-CN/03175f21-e441-4347-9f8e-bf8ecfbf8454/asplogin