简体   繁体   English

从PHP检测过期的asp.net会话

[英]Detecting an expired asp.net session from php

Firstly, please don't dismiss this question - I'm aware it's an ugly situation but hey, real life isn't pretty. 首先,请不要忽略这个问题-我知道这是一个丑陋的情况,但嘿,现实生活并不美好。

I'm developing an extra section to a web app that's written in asp.net, but in php - it's mostly done (the two parts don't really communicate with each other outside of a database - the integration is mostly just cosmetic.) 我正在为用asp.net编写的Web应用程序开发一个额外的部分,但是使用php-大部分已经完成了(这两个部分在数据库外部并没有真正的相互通信-集成主要是装饰性的。)

The only issue I have is detecting from the php part when the .net session has expired so that it logs the user out and redirects to the login page. 我唯一的问题是.net会话到期时从php部分进行检测,以使用户注销并重定向到登录页面。

I believe the asp.net application is compiled, but either way I'm not allowed to alter it so I was thinking maybe the best thing to do would be to make a very small/simple aspx page that outputs true or false which I could call using curl from php (and passing the browser's cookies along.) 我相信asp.net应用程序已编译,但是无论哪种方式,我都不允许更改它,因此我认为也许最好的办法是制作一个很小/简单的aspx页面,以输出true或false,我可以使用php中的curl调用(并传递浏览器的cookie。)

Would this even be possible? 甚至有可能吗? I'm not sure how session security works on asp.net eg whether one .net application can read another's session variables, but if it's anything like php then it'll be possible. 我不确定asp.net上的会话安全性如何工作,例如,一个.net应用程序是否可以读取另一个的会话变量,但是如果它像php一样,那么它是可能的。

mypage.php --curl--> checksession.aspx --|
|                                        |
<----------- true / false <---------------

So mypage issues a GET (with cookies from browser) to checksession using curl, checksession simply returns a true or false (or something like that) and mypage redirects to the site's login page if that's false. 因此,mypage使用curl发出一个GET(带有来自浏览器的cookie)到checksession,checksession只返回true或false(或类似的东西),如果为false,mypage将重定向到站点的登录页面。

The authentication for the php side is already sorted out and is separate to this issue. php端的身份验证已经解决,并且与此问题分开。

So really, what I need to know is can I have just a simple .aspx file that does this check, and if so where would I go to to find out how to program such a simple page? 因此,实际上,我需要知道的是我是否可以执行此检查的简单.aspx文件;如果是,我将去哪里找到如何编写此类简单页面的程序? If it's just a line or three, please could you let me know what those lines would be (I'm sorry I've never done any .net stuff..) 如果只是三三行,请您让我知道这些行是什么(对不起,我从未做过任何.net的工作。)

If this isn't possible, then if you don't mind, could you provide some alternative solutions? 如果这不可能,那么如果您不介意,是否可以提供一些替代解决方案? Thanks! 谢谢!

-- EDIT -- -编辑-

After spending most of the day with this problem, I'm now thinking that using php at all to get around this is a bad idea. 在花了一天的大部分时间解决这个问题之后,我现在认为完全使用php解决这个问题是一个坏主意。 There's actually two levels of authentication involved (one is normal HTTP request/response login type thing, then there's the .net session) - on top of that I totally missed the point that obviously these sessions are almost certainly going to be backed up by the IP of the users browser which I'd have to spoof or something coming from curl as that'll be running on the server. 实际上涉及身份验证的两个级别(一个是正常的HTTP请求/响应登录类型,然后是.net会话)-最重要的是,我完全错过了一点,显然这些会话几乎肯定会得到备份。我必须欺骗的用户浏览器的IP或来自curl的东西,因为它们将在服务器上运行。

So I think I'm going to use jQuery somewhere in the header of my page to check and redirect as required...somehow :/ 所以我想我将在页面标题中的某处使用jQuery来按需检查和重定向...以某种方式:/

-- EDIT 2 -- Ok, so the javascript way has suited my needs pretty well - obviously it's not a secure way of doing things, but fortunately in this case it's ok as this is just an app used on the intranet (and the shoddy way they authenticate users is terrible anyway.) -编辑2-好的,所以javascript方式非常适合我的需求-显然,这不是一种安全的方式,但是幸运的是,在这种情况下,因为这只是Intranet上使用的应用程序,所以还可以无论如何,他们对用户进行身份验证的方式非常糟糕。)

There are multiple ways here. 这里有多种方式。 First of all, the cookies alone won't do much, because the session can be expired in ASP.net even though the Cookie is still alive. 首先,仅cookie不会做很多事情,因为即使cookie仍然存在,会话也可以在ASP.net中过期。

ASP.net supports multiple SessionStates , of which two are common: ASP.net支持多个SessionState ,其中两个是常见的:

  1. In Process - here, the Web Server (Usually IIS) holds all Sessions in memory. 处理中-在这里,Web服务器(通常为IIS)将所有会话保存在内存中。 If the IIS Service is restarted (Happens by default every 24 hours, not just when the system restarts!), All Sessions are wiped (that's why the cookie alone does not help). 如果重新启动IIS服务(默认情况下每24小时发生一次,而不仅仅是系统重新启动时才发生!),则将擦除所有会话(这就是单独使用cookie不能解决问题的原因)。 This scenario is default I believe and thus very common in single-server environments 我认为这种情况是默认的,因此在单服务器环境中非常常见
  2. SQL Server - here, the session data is stored in a SQL Server database. SQL Server-此处,会话数据存储在SQL Server数据库中。 This isn't used that often though. 不过这种情况并不常用。
  3. State Server. 状态服务器。 This seems really uncommon, so I'm omitting it. 这似乎并不常见,因此我省略了。

Scenario 2 is your best bet, because you can just read the session id from the cookie and query the SQL Server database for the session info. 方案2是最好的选择,因为您可以从cookie中读取会话ID,并在SQL Server数据库中查询会话信息。

in Scenario 1, your approach is the correct one: You need help from the ASP.net Application, which can be a .aspx page. 在方案1中,您的方法是正确的:您需要ASP.net应用程序的帮助,该应用程序可以是.aspx页。 Make a request from PHP passing in the session cookie and check Session.SessionID and Session.IsNewSession to make sure the ID Matches and IsNewSession should be false - otherwise, ASP.net just recreated a new Session. 向PHP发出请求,传入会话cookie并检查Session.SessionIDSession.IsNewSession以确保ID Matches和IsNewSession应该为false-否则,ASP.net只是重新创建了一个新Session。

You may have to interact with the Session from the ASP.net page to activate it ( Session["PingFromPHP"] = true ) which may interfere with the ASP.net Application if your key (the name in the []-brackets) has the same name. 可能需要从ASP.net网页会话交互来激活它( Session["PingFromPHP"] = true ),这可能与ASP.net应用干扰,如果你的键([] -brackets名)有同名。

If the ASP.net Application is compiled, put the .aspx.cs file that serves as the code behind into a folder named App_Code , this should allow you to run it. 如果已编译ASP.net应用程序,请将用作代码的.aspx.cs文件放在名为App_Code的文件夹中,这将允许您运行它。

Hope that gets you started. 希望能帮助您入门。

我不知道php,但是如果两个应用程序都在同一个域中,则您应该能够读取ASP.Net会话cookie并确定它是否已过期。因为cookie会出现在域中,而不是使用它的Web应用程序中,所以浏览器一定会随请求提供cookie。

This maybe isn't the right answer to this question.. but I found a fairly easy (slightly ugly) fix which was a bit of luck really.. 这可能不是这个问题的正确答案..但是我发现一个相当简单(略难看)的修复程序,确实有点运气。

I created a page called checksession.aspx and asp.net (or the iis app) automatically redirected that page to the login page if the session had expired. 我创建了一个名为checksession.aspx的页面,如果会话已过期,asp.net(或iis应用程序)会自动将该页面重定向到登录页面。 Using this with curl I can now just check whether I get a 302 redirect and if so I know that the session has expired. 现在,我可以使用curl与curl一起检查是否获得302重定向,如果知道,则该会话已过期。

Woot :) 呜呜:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM