-1

我有一个用于用户名和密码的 2 个文本框和一个按钮。我想允许经过身份验证的用户登录并重定向到另一个页面并显示欢迎身份验证的用户名。或者显示消息无效的用户名。如何使用会话执行此操作并连接到数据库。如何使用数据库进行配置。请帮我。

My code is

        <div id="loginbox">
            <div align="center" style="width: 100%;">


            </div>--%>
            <br />

                        <label for="username">Username:</label> <input type="text"   
id="username" /></li>
   <label for="password">Password:</label> <input type="password" id="password" />

<input type="button" runat="server" value="Login">


</form>

My .cs code

protected void btnLogin_onclick(object sender, EventArgs e)
{
    string connect = "Data Source=ST0022;Initial Catalog=QuickMove_Globe;Persist   
Security Info=True;User ID=sa;Password=good";
    string query = "Select Count(*) From Common.Users Where UserID = ? And Password =   
?";
    int result = 0;
    SqlConnection con= new SqlConnection(connect);
        SqlCommand cmd = new SqlCommand(query, con);

            cmd.Parameters.AddWithValue("UserID",username.Value);
            cmd.Parameters.AddWithValue("Password", password.Value);
            con.Open();
            Session["User"] = username.Value;
            result = (int)cmd.ExecuteScalar();

    if (result > 0)
    {
        Response.Redirect("TestPortalPage.aspx");
    }
    else
    {
        lbluser.Text = "Invalid credentials";
    }


}
4

3 回答 3

1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Web.Security;
public partial class Account_Login : System.Web.UI.Page
{
SqlConnection conn  = new SqlConnection  ("Data Source=.\\SQLEXPRESS; AttachDbFilename=E:\\Raugh\\asp\\Login\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LoginButton_Click(object sender, EventArgs e) //click event of login button
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT u_id, u_pass from [user] where u_id = '"+UserName.Text+"' and u_pass='"+Password.Text+"'");
    cmd.Connection = conn;
    SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read() == true)
    {
        Session.Timeout = 1;
        Response.Redirect("~/Default.aspx");
        //Response.Write("<script Language='JavaScript'> alert('Logged In')</script>");
        conn.Close();
    }
    else
    {

        Response.Write("<script Language='JavaScript'> alert('Log In Failed')</script>");
        conn.Close();
    }
}

}

于 2013-06-13T09:12:13.253 回答
0

检查这篇文章

http://csharpdotnetfreak.blogspot.com/2012/06/login-page-form-example-in-aspnet.html

通过会话传递用户名很简单

Session["User"] = username;

在重定向页面中,在您放置的任何控件中使用它,例如,如果它是一个标签

if(Session["User"]!=null)
{
label1.Text=Session["User"].ToString();
}
//    else
//  {
//    label1.Text="Invalid Username;
//    }

else 在您的情况下不需要条件,因为您将在凭据匹配时重定向。

你的代码应该是

string strCon = "Data Source=ST0022;Initial Catalog=QuickMove_Globe;Persist
Security Info=True;User ID=sa;Password=good"; string strSelect = "SELECT COUNT(*) FROM youtablename WHERE yourcolumnname = @Username AND yourcolumnname = @Password";

于 2012-11-07T10:30:39.177 回答
0

此页面可能会帮助您走上正轨。

于 2012-11-07T09:44:18.843 回答