Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get object attributes from a session

I have a class named "admin" in my asp.net C# project.
It is:

public class Admin
{  
    private int ID;
    private string FirstName, LastName, IsMainAdmin, Email, Username, 
            Password,BirthDate, EntryDate;  

    public int id
    {
        get { return ID; }
        set { ID = value; }
    }

    public string firstname
    {
        get { return FirstName; }
        set { FirstName = value; }
}

    public string lastname
    {
        get { return LastName; }
        set { LastName = value; }
    }
    .  
    .  
    .  

After login a session is created like this:

Admin admin = isAdmin(username, password);
    if (admin != null)
    {
        **Session.Add("maskanAdmin", admin);**
        Response.Redirect("panel.aspx");
    }

In other page i need to get admin's ID from session in code behind section after page request via jquery ajax.
Please notice that my code behind Method is [WebMethod] that is not supporting Session Object.
Can i get it? How?

like image 624
Ali Sohrabi Avatar asked Jan 12 '23 17:01

Ali Sohrabi


1 Answers

var adminObj = (Admin)Session["maskanAdmin"];    
if(adminObj != null)
{
    var id = adminObj.id;
    var fname = adminObj.firstname;
}

Read more about Read Values from Session State

Update

I am not sure why the question is updated after one hour saying you are using the code in web methods.

However, have a look at Using ASP.NET Session State in a Web Service

like image 67
huMpty duMpty Avatar answered Jan 21 '23 19:01

huMpty duMpty