asp.net登录功能

一、页面布局

 <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                                <tr>
                                    <td align="center" colspan="2">
                                        登录</td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                            ControlToValidate="UserName" ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" 
                                            ValidationGroup="Login2" Display="Dynamic">账号不能为空</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"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                            ControlToValidate="Password" ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" 
                                            ValidationGroup="Login2" Display="Dynamic">密码不能为空</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登录" 
                                            ValidationGroup="Login2" onclick="LoginButton_Click" />
                                    </td>
                                </tr>
                            </table>

二、编辑配置文件

<authentication mode="Forms">
//defaultUrl登录成功跳转的页面,loginUrl登录失败跳转的页面
        <forms defaultUrl="TaskList.aspx"  loginUrl="Login.aspx">
          <credentials passwordFormat="Clear">
//设置用户名和密码
            <user name="admin" password="admin"/>
          </credentials>
        </forms>
 </authentication>
    
    <location path="TaskList.aspx">
        <system.web>
          <authorization>
//允许登录的用户
            <allow users="admin"/>
//阻止登录的用户,*表示所有匿名用户
            <deny users="*"/>
          </authorization>
        </system.web>
     </location>

三、后台代码

首先要引用:using System.Web.Security;

 protected void LoginButton_Click(object sender, EventArgs e)
        {
            if (FormsAuthentication.Authenticate(UserName.Text,Password.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(UserName.Text,true);
            }
            else
            {
                Response.Write("<script>alert('账号或密码不正确');location.href='Login.aspx'</script>");
            }
        }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值