![](/img/trans.png)
[英]How to prevent auto login after registration in ASP.Net Web Forms (Identity)?
[英]how to auto login after registration in asp.net
我想在注冊后使用Session [“ ud”]這樣的會話 自動登錄,但是我不知道該放在哪里。
public partial class index : System.Web.UI.Page
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["dbpath"]);
protected void btnSave_Click(object sender, EventArgs e)
{
long idx;
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "Insert into tblUser (UInfo,UEmail,UName,UPass, UGender) Values (@P1,@P2,@P3,@P4,@P5) select @@Identity";
cmd.Parameters.AddWithValue("@P1", txtInfo.Text);
cmd.Parameters.AddWithValue("@P2", txtEmail.Text);
cmd.Parameters.AddWithValue("@P3", txtUserName.Text);
cmd.Parameters.AddWithValue("@P4", txtPass.Text);
cmd.Parameters.AddWithValue("@P5", rdbMale.Checked);
cnn.Open();
idx = Convert.ToInt64(cmd.ExecuteScalar()); // i think here we can do something
cnn.Close();
在這里我們要上傳用戶圖像,它可以正常工作
string fn = "";
if (FileUpload1.HasFile == true)
{
fn = FileUpload1.FileName;
string des = Server.MapPath("\\UserImg\\") + idx.ToString() + ".jpg";
FileUpload1.PostedFile.SaveAs(des);
SqlCommand cmdUpdate = new SqlCommand();
cmdUpdate.Connection = cnn;
cmdUpdate.CommandText = "Update tblUser Set UImg=@P5 where UId=@P0";
cmdUpdate.Parameters.AddWithValue("@P5", idx.ToString() + ".jpg");
cmdUpdate.Parameters.AddWithValue("@P0", idx);
cnn.Open();
cmdUpdate.ExecuteNonQuery();
cnn.Close();
}
Response.Redirect("Profile.aspx");
}
}
在sql數據庫中輸入數據后,您將在此處獲取新用戶的id
idx = Convert.ToInt64(cmd.ExecuteScalar()); // i think here we can do something
取得ID后,將其分配給會話
idx = Convert.ToInt64(cmd.ExecuteScalar()); // i think here we can do something
cnn.Close();
Session["ud"]=idx;
分配會話后,您只需重定向到所需頁面並驗證Session變量是否為null。
我希望在Profile.aspx頁面上您正在檢查相同的會話變量。
Profile.aspx.cs-頁面加載
if (Session["ud"] != null)
{
//successfull login
}
else
{
//redirect to login page
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.