如何以登录形式检查登录成功还是失败? [英] How can I check if login successful or failed in login form ?

查看:224
本文介绍了如何以登录形式检查登录成功还是失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,
我正在为我的网站使用登录表单,我想检查用户登录是否成功或登录失败并在标签中显示.那我该怎么做呢?我尝试使用选择陈述完成的陈述事件,但失败了. :(有帮助吗?

Hi guyz,
I am working on login form for my site and I wanna check if the user login is succsseful or failed to login and show it in label. So how can I do this? I tried to use the event of statment complete of selection statment but I faild . :( any help?

推荐答案

您可以将此代码用于将为您显示消息的登录控件.

You can use this code for the login control that will display message for you.

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
        <LayoutTemplate>
            <span class="failureNotification">
                <asp:Literal ID="FailureText" runat="server"></asp:Literal>
            </span>
            <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"

                 ValidationGroup="LoginUserValidationGroup"/>
            <div class="accountInfo">
                <fieldset class="login">
                    <legend>Account Information</legend>
                    <p>
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"

                             CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."

                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"

                             CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."

                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:CheckBox ID="RememberMe" runat="server"/>
                        <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                    </p>
                </fieldset>
                <p class="submitButton">
                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
                </p>
            </div>
        </LayoutTemplate>
    </asp:Login>


< pre lang ="c#">
/////登录验证/////////
受保护的void Button1_Click(对象发送者,EventArgs e)
{
SqlConnection con =默认值(SqlConnection);
SqlCommand cmd =默认值(SqlCommand);
SqlDataReader dr =默认值(SqlDataReader);
//种子定义
字符串uid = null;
uid = TextBox1.Text;
会话[用户名"] = uid;

字符串constr = ConfigurationManager.ConnectionStrings ["SomeDataBase"].ToString();
con =新的SqlConnection(constr);

con.Open();
cmd = new SqlCommand("select * from Reg",con);
dr = cmd.ExecuteReader();
字符串s = null;
字符串s1 = null;
while((dr.Read()))
{
s = dr ["Username"].ToString();
s1 = dr ["Password"].ToString();

如果((((TextBox1.Text == s.Trim())&(TextBox2.Text == s1.Trim())))
{
Response.Redirect("Home.aspx");

}
Label3.Visible = true;

}
con.Close();
}</pre>
<pre lang="c#">
///// login validate /////////
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = default(SqlConnection);
SqlCommand cmd = default(SqlCommand);
SqlDataReader dr = default(SqlDataReader);
// Seession define
string uid = null;
uid = TextBox1.Text;
Session["Username"] = uid;

string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
con = new SqlConnection(constr);

con.Open();
cmd = new SqlCommand("select * from Reg", con);
dr = cmd.ExecuteReader();
string s = null;
string s1 = null;
while ((dr.Read()))
{
s = dr["Username"].ToString();
s1 = dr["Password"].ToString();

if (((TextBox1.Text == s.Trim()) & (TextBox2.Text == s1.Trim())))
{
Response.Redirect("Home.aspx");

}
Label3.Visible = true;

}
con.Close();
}</pre>


如果您正在使用aspnet成员表
您可以轻松实现
使用登录控件
&如果出现LoginError
设置LoginError.FailureText

或者您可以在设计时添加它
if u are using aspnet membership tables
u could make it easy
use login control
& in the event of LoginError
set the LoginError.FailureText

or u could add it in desigin time


这篇关于如何以登录形式检查登录成功还是失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆