开发者

teaching about sessions?

Can any one teach me how to use sessions? I have a login page which refers to mysql db table named User which includes UserID="variable number", username and password. Id like to be able to store the userid in a session so I can call it inside the users profile when he logs in.

How is this done? Any relevant coding and placement of specific coding would be helpful as Im just not picking it up at all. Does it go inside the code behind (.cs file) or is it held in the html of the aspx page? or both and how? How does the session work across multiple inner pages (once logged in you can broswe more than one page related to your own profile)

    using System.Data.Odbc; 
    partial class login : System.Web.UI.Page 
    {        
    protected void   Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)     
    {         
    OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=mydb; User=root;Password=;");         
cn.Open();
         OdbcCommand cmd = new OdbcCommand(开发者_JS百科"Select * from login where username=? and password=?", cn);          
    //Add parameters to get the username and password            
    cmd.Parameters.Add("@username", OdbcType.VarChar);   
      cmd.Parameters["@username"].Value = this.Login1.UserName;

        cmd.Parameters.Add("@password", OdbcType.VarChar);  
       cmd.Parameters["@password"].Value = this.Login1.Password;  

        OdbcDataReader dr = default(OdbcDataReader);        
 // Initialise a reader to read the rows from the login table.   

  // If row exists, the login is successful        
    dr = cmd.ExecuteReader();        
  if (dr.HasRows) 
{             
e.Authenticated = true;             
// Event Authenticate is true       
    }    
  } 
}


In C#, to store a variable in a Session variable:

Session["usrID"]="myUserName";

Later, to retrieve the Session variable:

string usrName = Convert.ToString(Session["usrID"]);


http://msdn.microsoft.com/en-us/library/ms178581.aspx

Basically, give yourself a property for each variable in the page class, eg.

private int SelectedYear
{
    get 
    { 
        object obj = Session["SelectedYear"];
        if (obj != null)
        {
            return int.Parse(obj.ToString()); 
        }
        else
        {
            return 0;
        }
    }
    set { Session["SelectedYear"] = value; }
}


if you need something for user management, you should go with a membershipprovider. http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx

to get userinformation you can use

Membership.GetUser()

if you just want to use sessions to store variables, you can easily use

Session["name"] = value;
var value = Session["name"];

like others already mentionend


When should you call the code to set the Session variable value?

You can do that in the Login_Authenticate method. That it where you have first retrieved the ID.

Or, you can do it in the Global.asax file, in the Session_Start event. Then, you will always have the value available on every page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜