-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

このページはあなたを正しい軌道に乗せるのに役立つかもしれません。

于 2012-11-07T09:44:18.843 に答える
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 に答える