Asp.net Cookie或会话以及如何开始
Heey Stackoverflow 我有一个问题我开始学习asp.net语言csharp,我有以下登录代码我的问题是如何开始或在哪里可以学习写下会话cookie,然后我可以返回到另一个页面再次读取此cookie的用户名和密码非常匹配Asp.net Cookie或会话以及如何开始,asp.net,session,cookies,Asp.net,Session,Cookies,Heey Stackoverflow 我有一个问题我开始学习asp.net语言csharp,我有以下登录代码我的问题是如何开始或在哪里可以学习写下会话cookie,然后我可以返回到另一个页面再次读取此cookie的用户名和密码非常匹配 public partial class Administratie : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { }
public partial class Administratie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string cnnString = ConfigurationManager.ConnectionStrings["Stefan"].ConnectionString;
using (SqlConnection con = new SqlConnection(cnnString))
using (SqlCommand cmd = new SqlCommand("select [Username],[Password] from Admin where [Username] = @Username and [Password] = @Password", con))
{
string Username = (textUsername.Text.Length > 0) ? textUsername.Text : null;
string Password = (TextPassword.Text.Length > 0) ? TextPassword.Text : null;
cmd.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = textUsername.Text;
cmd.Parameters.Add("@Password", System.Data.SqlDbType.VarChar).Value = TextPassword.Text;
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
if (Page.IsValid)
{
// Login Succeed
// Response.Redirect("Admin.aspx");
}
}
}
}
catch (Exception) { }
// Login Failed
Response.Write("Wrong Username ");
}
}
试试看这里:
阅读Cookie:
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
lblWelcome.Text = "<b>Unknown Customer</b>";
}
else
{
lblWelcome.Text = "<b>Cookie Found.</b><br><br>";
lblWelcome.Text += "Welcome, " + cookie["Name"];
}
如果要在会话中存储数据,只需设置它:
Session["username"]=username;
并阅读:
string username=Session["username"];
试试看这里:
阅读Cookie:
HttpCookie cookie = Request.Cookies["Preferences"];
if (cookie == null)
{
lblWelcome.Text = "<b>Unknown Customer</b>";
}
else
{
lblWelcome.Text = "<b>Cookie Found.</b><br><br>";
lblWelcome.Text += "Welcome, " + cookie["Name"];
}
如果要在会话中存储数据,只需设置它:
Session["username"]=username;
并阅读:
string username=Session["username"];
您可以使用会话存储用户名和密码。如果你想使用“记住我”选项,你可以在cookies中保存用户名和密码
请检查使用链接,您可以使用会话存储用户名和密码。如果你想使用“记住我”选项,你可以在cookies中保存用户名和密码 请检查链接,以便使用和