登录经过身份验证的用户

时间:2012-11-07 09:39:11

标签: asp.net sql-server

我有一个用于用户名和密码的文本框和一个按钮。我想允许经过身份验证的用户登录并重定向到另一个页面并显示welcome authenticated username.Or else显示消息Invalid username.How如何使用session执行此操作并连接到database.How配置数据库。请帮帮我。

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";
    }


}

3 个答案:

答案 0 :(得分: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();
    }
}

}

答案 1 :(得分:0)

This page可能会帮助您走上正确的轨道。

答案 2 :(得分: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";