惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
量子位
V
V2EX
S
SegmentFault 最新的问题
月光博客
月光博客
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
博客园_首页
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
Y
Y Combinator Blog
The Cloudflare Blog
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
B
Blog
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed

博客园 - 张明

搭建windows mobile 6 中文开发环境和中文测试环境 asp.net2.0为DataList和GridView内容项添加序号 [原创]完美Js(Javascript)代码自动成器 金额转换,把金额转换成为汉字大写金额! 使用存储过程并返回值与及返回值的获得方法 AjaxControlToolkit的安装与使用详解 更新公告,本Blog从即日起开始更新,基本保证每天一更,欢迎广大开发者提出宝贵意见 IFRAME滚动条的样式 asp.net下Split分隔字符串的应用 - 张明 - 博客园 [原创]Asp.net2.0下(数据库主表,子表)实现TreeView控件的树型目录。 [原创]Asp.net2.0下利用Global.asax全局文件实现流量分析 [原创]Asp.net2.0 VS 2005下的repeater控件本功能分页实例(共有 条记录 共有几页 当前第 页 首页,上一页,下一页,尾页 DropDownList跳转) File文件控件,选中文件(图片,flash,视频)即立即预览显示 ADO.NET 的最全实例,Ado.Net最佳使用方法 ADO.NET 的最佳实践技巧 ASP.NET结合存储过程写的通用搜索分页程序 GridView记录删除前的确认窗口 asp.net2.0 GridView Url传递中文参数解决方案 MsSqlServer常用数据类型 常用正则表达式
Asp.Net2.0下C#环境 Login控件实现用户登录
张明 · 2008-04-29 · via 博客园 - 张明

一、前台显示效果

二、前台代码

            <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
                BorderWidth
="1px" Font-Names="Verdana" Font-Size="10pt" OnAuthenticate="Login1_Authenticate">
                
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                
<LayoutTemplate>
                    
<table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
                        
<tr>
                            
<td>
                                
<table border="0" cellpadding="0">
                                    
<tr>
                                        
<td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #6b696b">
                                            用户登录
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="UserName" runat="server" CssClass="STYLE1"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                ErrorMessage
="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="STYLE1"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                ErrorMessage
="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="right">
                                            
<asp:Label ID="Label1" runat="server" AssociatedControlID="Password">验证码:</asp:Label></td>
                                        
<td>
                                            
<asp:TextBox ID="GetCode" runat="server"  CssClass="STYLE2"></asp:TextBox>
                                            
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Password"
                                                ErrorMessage
="必须填写“验证码”。" ToolTip="必须填写“验证码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator><asp:Image ID="Image1" runat="server" ImageUrl="~/include/GetValidate.aspx" ImageAlign="Top"
                                                                            alt
="看不清?点击更换" onclick="this.src=this.src+'?'" />
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="center" colspan="2" style="color: red">
                                            
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                        
</td>
                                    
</tr>
                                    
<tr>
                                        
<td align="center" colspan="2">
                                            
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登录" ValidationGroup="Login1" /></td>
                                    
</tr>
                                
</table>
                            
</td>
                        
</tr>
                    
</table>
                
</LayoutTemplate>
            
</asp:Login>

三、后台代码

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    
{
        TextBox GetCode 
= Login1.FindControl("GetCode"as TextBox;//获取登陆控件中验证码文本框值
        if (Request.Cookies["CheckCode"].Value == null)
        
{
            Response.Write(
@"<script language=JavaScript>{window.alert('您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统!');}</script>");
            
return;
        }

        
else
        
{
            
if (String.Compare(Request.Cookies["CheckCode"].Value, GetCode.Text.ToString().Trim(), true!= 0)
            
{
                Response.Write(
@"<script language=JavaScript>{window.alert('验证码输入不正确!');}</script>");
                
return;
            }

            
string UserLoginID = Login1.UserName.ToString().Trim().Replace("'""").Replace("=""");//得到输入的用户名
            string UserLoginPwd = Login1.Password.ToString().Trim().Replace("'""").Replace("=""");//得到输入的密码
            
//得到md5值
            string md5Pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(UserLoginPwd, "md5").ToLower();
            
string mySql = "select * from [User] where [sLoginID]='" + UserLoginID + "' and [LoginPWD]='" + md5Pwd + "'";
            
//下面部署自己的逻辑处理,以下仅供参考
            DBConn myDB = new DBConn();
            
try
            
{
                SqlDataReader Rs 
= myDB.getDataReader(mySql);
                
if (!Rs.Read())
                
{
                    e.Authenticated 
= false;//登录不通过
                }

                
else
                
{
                    Session[
"UserLoginID"= UserLoginID;
                    Session[
"UserLoginPwd"= UserLoginPwd;
                    e.Authenticated 
= true;//登录通过
                    Response.Redirect("index.aspx");
                }

            }

            
catch (Exception ex)
            
{
                Response.Write(
"数据库错误,错误原因:" + ex.Message);
                Response.End();
            }


        }

    }

 四、关于验证码可以本Blog的随笔中找到,地址为:

http://www.cnblogs.com/zm235/archive/2006/10/02/520233.html