Save form data in session

Asked

Viewed 1,169 times

3

I created an access form for my administrative area, using a tutorial by Microsoft.

Now I need to retrieve the login that he typed to enter and show it on an administration page, and also to make future queries to the database.

How can I do that? I was told to save the login and password in one Session and then retrieve it on the other page, but I have no idea how to implement.

2 answers

4

Sessions are ways to store data that will be deleted when a given event occurs (in the default behavior of ASP.NET, when 20 minutes have passed since the user’s last action, or when the browser window is closed).

Creating a Session couldn’t be easier:

Session["NomeDaSession"] = "ValorDaSession";

Check the value of it also:

// Primeiro verificamos se a session existe
if (Session["NomeDaSession"] != null)
    // Operações pertinentes

However, in addition to answering your question, it is up to me to recommend the use of a more recent technology, if you are working on something new. I know that, for legacy, there is usually no way. Instead of Web Forms, try to use ASP.NET MVC where relevant.

  • The company where I do internship uses Webforms :/

3


When you’re using FormsAuthentication you will be able to recover the logged in user in this way:

string username = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
    username = HttpContext.Current.User.Identity.Name;
}
  • username = xxx <--- There is a question there with a closure of ).

  • Thanks Iuri, you saved the/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.