如何从会话中获取对象属性

Ali*_*abi 5 c# asp.net session

我的asp.net C#项目中有一个名为"admin"的类.
它是:

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; }
    }
    .  
    .  
    .  
Run Code Online (Sandbox Code Playgroud)

登录后会创建一个会话,如下所示:

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

在其他页面中,我需要通过jquery ajax在页面请求之后的代码后面的会话中获取管理员的ID.
请注意,我在Method后面的代码是[WebMethod],它不支持Session Object.
我能得到它吗?怎么样?

huM*_*pty 7

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

阅读有关从会话状态读取值的更多信息

更新

我不确定为什么问题会在一小时后更新,说您正在使用Web方法中的代码.

但是,请查看在Web服务中使用ASP.NET会话状态