コード例 #1
0
        private string CheckSession()
        {
            string sessionId = "";

            try
            {
                ServerCollection servers = new ServerCollection(Path.Combine(
                                                                    Request.PhysicalApplicationPath,
                                                                    "App_Data",
                                                                    "Servers.xml"
                                                                    ));

                string server;

                if (Request.Cookies["ASP.NET_SessionId"] != null)
                {
                    sessionId = Request.Cookies["ASP.NET_SessionId"].Value;

                    if (!Global.Sessions.ContainsKey(sessionId))
                    {
                        //sessionId = GenerateNewSessionId();
                        //Request.Cookies.Remove("ASP.NET_SessionId");
                        //Request.Cookies.Add(new HttpCookie("ASP.NET_SessionId", sessionId));

                        server = servers.GetServer(Request.ServerVariables["REMOTE_HOST"]);
                    }
                    else if (servers.Items.ContainsKey(Global.Sessions[sessionId]) == false ||
                             servers.Items[Global.Sessions[sessionId]].State == ServerState.Offline)
                    {
                        server = servers.GetServer(Request.ServerVariables["REMOTE_HOST"]);
                    }
                    else
                    {
                        server = Global.Sessions[sessionId];
                    }

                    if (server == null)
                    {
                        ShowOverloadMessage();
                        return(null);
                    }
                }
                else
                {
                    server = servers.GetServer(Request.ServerVariables["REMOTE_HOST"]);

                    if (server == null)
                    {
                        ShowOverloadMessage();
                        return(null);
                    }

                    ServiceLink service = new ServiceLink(string.Format(
                                                              "http://{0}/Handlers/GlobalHandler.ashx",
                                                              server
                                                              ));

                    /*Dictionary<string, string> cookies = new Dictionary<string, string>();
                     *
                     * cookies.Add("ASP.NET_SessionId", GenerateNewSessionId());*/


                    sessionId = service.Request(new string[]
                    {
                        "Method=CreateSession"
                    });

                    Request.Cookies.Add(new HttpCookie("ASP.NET_SessionId", sessionId));
                }

                if (!Global.Sessions.ContainsKey(sessionId))
                {
                    Global.Sessions.Add(
                        sessionId,
                        server
                        );
                }
                else
                {
                    Global.Sessions[sessionId] = server;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                Response.End();
                return(null);
            }

            return(sessionId);
        }