简体   繁体   English

在asp.net中注册后如何自动登录

[英]how to auto login after registration in asp.net

I want to login automatically after registration by using a session like Session ["ud"] , but I don't know where should I put it. 我想在注册后使用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();  

here we want to upload the image of user and it works correctly 在这里我们要上传用户图像,它可以正常工作

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

once you have entered data into in sql database you will get id of new user here 在sql数据库中输入数据后,您将在此处获取新用户的id

 idx = Convert.ToInt64(cmd.ExecuteScalar()); // i think here we can do something

Once you get the id assign it to your session 取得ID后,将其分配给会话

idx = Convert.ToInt64(cmd.ExecuteScalar()); // i think here we can do something
 cnn.Close();  
 Session["ud"]=idx;

once you have assigned session ,you just have to redirect to required page and validate Session variable if it's null or not. 分配会话后,您只需重定向到所需页面并验证Session变量是否为null。

i hope on Profile.aspx page you are checking for same session variable. 我希望在Profile.aspx页面上您正在检查相同的会话变量。

Profile.aspx.cs--on page load Profile.aspx.cs-页面加载

if (Session["ud"] != null)
                {
                    //successfull login
                }
                else
                {
                    //redirect to login page
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM