0

I have created a session for User Login as:

[HttpPost]
public ActionResult Index(UserLogin lc)
{
    string mainconn = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(mainconn);
    SqlCommand sqlcomm = new SqlCommand("UserLogin");
    sqlconn.Open();
    sqlcomm.Connection = sqlconn;
    sqlcomm.CommandType = CommandType.StoredProcedure;
    sqlcomm.Parameters.AddWithValue("@Email", lc.Email);
    sqlcomm.Parameters.AddWithValue("@Password", lc.Password);
    SqlDataReader sdr = sqlcomm.ExecuteReader();
    
    if(sdr.Read())
    {
        FormsAuthentication.SetAuthCookie(lc.Email, true);
        Session["Email"] = lc.Email.ToString();
        return RedirectToAction("Welcome");
    }

Now I need to use the Session variable stored here in another controller. How can I do so?

2
  • Have you tried var email = Session["Email"];? Commented Sep 26, 2022 at 15:25
  • This looks a lot like you're storing plain-text passwords in the DB, which is very much not okay. It's one of those things you shouldn't even do for practice/learning/proof of concept projects. Commented Sep 26, 2022 at 15:26

1 Answer 1

0

Session Variable works with HTTP Request you'll get the session variable in any controller inherited from BaseController after once you have set the Session variable key value.As Session is a Dictionary Type Object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.