在Web服务中获取并设置会话

时间:2016-05-28 11:16:51

标签: c# web-services session

我有处理登录和注销的Web服务,在Web服务中我设置会话值和获取会话。登录

    [WebMethod(EnableSession = true)]
    public object LoginByEmail(string Email, string Password, int RegionID)
    {
        try
        {
            Guid? UserID = null;

            int? res = TBll.LoginByEmail(ref UserID, Email, Password, RegionID);

            if (res == 1)
                return new { Result = resultEnum.notcorrectemailorpassword };

            else if (res == 2)
            {
                Session["User"] = UserID;
                return new { Result = resultEnum.ok, UserID = UserID };
            }




            else if (res == 3)
                return new { Result = resultEnum.accountnotactive };
            else

                return new { Result = resultEnum.error };

        }
        catch
        {
            return new { Result = resultEnum.error };
        }

    }

当我转到会话中的定义时(会话[“用户”] =用户ID;)在以前的代码中 Session Property

    [WebMethod(EnableSession = true)]
    public object CheckIfLoggedin()
    {
        try
        {
            return new { Result = resultEnum.ok, Records = GetUserID() };

        }
        catch
        {
            return new { Result = resultEnum.error };
        }

    }

    [WebMethod(EnableSession = true)]
    private Guid? GetUserID()
    {

        if (Context.Session["User"] != null)
        {
            string userid = Context.Session["User"].ToString();
            if (userid != "")
                return new Guid(userid);
            else
                return null;
        }
        else
            return null;

    }

在Get I中使用Context.Session(字符串userid = Context.Session [“User”]。ToString();)如果我去定义

enter image description here

我的问题是两者有什么不同?什么是正确的属性用于在Web服务中设置和获取会话?

0 个答案:

没有答案