I have created a class that returns a class object which have info about the username,pwd,emailid of current user.
Now i want to use this email id in my page to show the current logged in user and its email id....
i m creating an isntance of that class and using that to get the user email...
but i m getting null...

anyone had any idea?

Thanks in Advance,

Dani AI

Generated

The myUser instance created during the login request does not survive a Response.Redirect; that object lives only for the current HTTP request. That explains why the email is null on the redirected page. As noted, the value must be persisted across requests or reloaded on the new request.

Recommended approaches for ASP.NET 2.0 (pros / cons and notes):

  • Use the Membership API when available: it keeps authentication separate from user data and avoids storing sensitive fields in memory. Example retrieval on the redirected page:

    if (User.Identity.IsAuthenticated) {
        var mu = System.Web.Security.Membership.GetUser(User.Identity.Name);
        var email = mu != null ? mu.Email : null;
    }
  • Store a small value in Session (simple, common). Store only the ID or email, not passwords. Remember: objects put in session must be [Serializable] when using StateServer/SQLServer; avoid large objects if the site runs in a web farm.

    // after successful validation
    Session["UserEmail"] = myUser.Email;
    Response.Redirect("Default.aspx");
    
    // on destination page
    var email = Session["UserEmail"] as string;
  • Use a FormsAuthenticationTicket UserData for a lightweight, encrypted, cookie-backed value if a stateless approach is desired. Keep UserData small.

  • Use Server.Transfer if the goal is to preserve page context for a single server-side transfer; PreviousPage can expose public properties.

Troubleshooting checklist: confirm authentication cookie is set, check User.Identity.IsAuthenticated before calling Membership.GetUser, verify session keys are set before redirect, ensure session mode is compatible with stored objects, and never keep plaintext passwords in session/cookies. For most ASP.NET 2.0 scenarios the safest pattern is to authenticate (FormsAuthentication), keep the username in the ticket, and read email from the Membership/Profile provider or the database when the next page runs.

Recommended Answers

All 2 Replies

my class is like this

 public myUser ValidateUser(string userName,string password)
        {
//validates user with UN and pwd
}
return myUser;

where myUser keeps UN,pwd, emailid

Now i have an aspx.cs page which is getting redirected when teh above class verifies it since above class is used in login page

Now i m getting all teh data in return myUser;
I wnat to use it in the redirected page....
I don't think any viewstate or server variables are needed...is it so...?

you can either run the class again to retrieve it again, you can put the information into a querystring that goes to the new page, put it in session variables, put it in cookies, put it into files, or put it into a form, and post the form to the new page.

There's many options

Or.. you can just validate on the new page, and redirect back if it fails?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.