gpt4 book ai didi

c# - 实现一个 session 并从 mysql 表中检索?

转载 作者:行者123 更新时间:2023-11-29 09:11:54 28 4
gpt4 key购买 nike

如何实现此 session :(UserID 是登录表的一部分)

Session["UserID"]="usrName";

进入这段代码?

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Login1.Authenticate += Login1_Authenticate;
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
//database connection string
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=x; Password=x; OPTION=3;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("Select * from User where username=? and password=?", cn);

//Select the username and password from mysql database in login table

cmd.Parameters.Add("@username", OdbcType.VarChar);
cmd.Parameters["@username"].Value = this.Login1.UserName;

cmd.Parameters.Add("@password", OdbcType.VarChar);
cmd.Parameters["@password"].Value = this.Login1.Password;
//use asp login control to check username and password
cmd.Parameters.Add(new OdbcCommand("@UserID", 'id.int'));


OdbcDataReader dr = default(OdbcDataReader);
// Initialise a reader to read the rows from the login table.
// If row exists, the login is successful

dr = cmd.ExecuteReader();
int id = cmd.Parameters["@UserID"].Value;
Session["UserID"]="usrName";

if (dr.Read())
{
e.Authenticated = true;
Response.Redirect("UserProfileWall.aspx");
// Event Authenticate is true forward to user profile
}

}
}

当有人在登录时输入用户名和密码时,我需要能够检索正确的 UserID,然后如何在新页面上检索它,就像这样将其停用但不确定?

string usrName = Convert.ToString(Session["UserID"]);

我只是不知道如何将第一部分(即 session )添加到我的登录代码中,这样我就可以了解如何将 UserID 存储在我的 session 中,同时也可以从我的 mysql 登录表中提交的数据中检索正确的 UserID。

最佳答案

这很脏,而且是错误的,但我想这在紧要关头会起作用。

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

{
var sql = @"SELECT * from Users where userName = ?user and password = ?pass";
DataSet ds = new DataSet();

MySqlCommand commandWithParams = new MySqlCommand(sql);

commandWithParams.Parameters.AddWithValue("?user", Login1.UserName);
commandWithParams.Parameters.AddWithValue("?pass", Login1.Password);

MySqlConnection conn = new MySqlConnection("myconn string");

if (conn.State != ConnectionState.Open)
conn.Open();

commandWithParams.Connection = conn;


MySqlDataAdapter da = new MySqlDataAdapter(commandWithParams);

da.Fill(ds);

DataTable dt = ds.Tables[0];
DataRow dr = dt.Rows[0];

conn.Close();
conn.Dispose();
da.Dispose();

if (dt.Rows.Count != 0)//I'm sure this has a better way
{
Session["userId"] = Convert.ToString(dr["userId"]);
Session["userName"] = Convert.ToString(dr["userName"]);
e.Authenticated = true;
Reponse.Redirect("your_page.aspx");
}
else
{
e.Authenticated = false;
}
}

这里再次使用 Reader 方法和 Odbc。

        OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=x; Password=x; OPTION=3;");
cn.Open();

OdbcCommand cmd = new OdbcCommand("Select * from users where username=? and password=?", cn);

cmd.Parameters.Add("@username", OdbcType.VarChar);
cmd.Parameters["@username"].Value = "test";

cmd.Parameters.Add("@password", OdbcType.VarChar);
cmd.Parameters["@password"].Value = "test1";


OdbcDataReader dr = default(OdbcDataReader);

dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string theUser = (string)dr["userName"];
string theUserId = Convert.ToString(dr["userId"]);
}

您可以将 theUsertheUserId 设置到 session 中,如下所示:

Session.Add("userName", theUser);
Session.Add("userId", theUserId);

关于c# - 实现一个 session 并从 mysql 表中检索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373567/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com