C# 实现一个会话并从mysql表中检索?
如何实现此会话:(UserID是登录表的一部分) 这个密码C# 实现一个会话并从mysql表中检索?,c#,asp.net,mysql,html,C#,Asp.net,Mysql,Html,如何实现此会话:(UserID是登录表的一部分) 这个密码 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 p
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
}
}
}
我需要能够检索到正确的用户ID时,有人输入用户名和密码登录,然后一些如何检索它在一个新的页面上类似这样的东西退休,但不确定
string usrName = Convert.ToString(Session["UserID"]);
我只是不知道如何将会话的第一部分添加到我的登录代码中,这样我就可以在会话中存储用户ID,但也可以从mysql登录表中提交的数据中检索正确的用户ID。这是脏的,也是错误的,但我想这在必要的时候会起作用
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"]);
}
您可以将user
和userid
设置为会话,如下所示:
Session.Add("userName", theUser);
Session.Add("userId", theUserId);
没有帮助?我的问题是否足够清楚或可以理解?这也是100%未经测试的,我将两种不同的方法拼凑在一起。因此,请将其视为数据提取的价值所在。但如果这是你为用户身份验证所做的一切,那么我认为你做错了。它只是将asp.nets登录身份验证方法转换为使用mysql而不是mssql,我看没有问题吧?此外,我认为这是错误的,我不知道为什么要使用datarow来检索用户ID。我也不希望我的方法包含在公共int中,它必须在代码落后的情况下完成,但就此而言,为什么不将此逻辑放在codebehind中的方法中呢?这是一个简单的单行程序,来自您的登录方法,然后
intuserid=GetUserId(Login1.UserName,Login1.Password)
然后您可以对int执行任何您想要的操作,或者将其传递到会话中。然后从会话中提取它string userId=session(“userId”)。ToString()
Session.Add("userName", theUser);
Session.Add("userId", theUserId);