来自DLL的会话变量值

时间:2013-04-23 09:58:57

标签: c# asp.net web-services session

我有一个调用DLL的Web应用程序,这个DLL从Web服务获取一些数据。我想要做的是当用户登录到网络应用程序时,我目前将其用户名存储在会话变量中。在我的webService中,我有一个像这样的WebMethod:

    [WebMethod(EnableSession = true)]
    public string getLoggedInUsername()
    {
        SessionManager session = new SessionManager();

        return session.Username;
    }

这很好用,但是当我从DLL访问web服务时,返回的值总是NULL,这是因为当控制台应用程序连接到webservice时,将初始化一个新会话。有什么替代方法我如何存储登录的用户名并通过我的webservice传递给dll?使用应用程序获取/设置不是一个好的决定,因为我的webapp需要处理不同的用户。有人能指出我正确的方向吗?

SessionManager类包含以下内容:

    public string Username
    {
        get
        {
            return (string)HttpContext.Current.Session["Username"];
        }
        set
        {
            HttpContext.Current.Session["Username"] = value;
        }
    }

用户登录我的应用程序后(我使用asp.net会员资格):我以这种方式将用户名分配给会话变量:

            SessionManager session = new SessionManager();
            session.Username = User.Identity.Name;

感谢。

1 个答案:

答案 0 :(得分:0)

查看此文章:http://www.codeproject.com/Articles/35119/Using-Session-State-in-a-Web-Service 您可能必须重新初始化代理和cookie容器