[PHP/.NET]如何把PHP站点用户登录会话传递到ASP.NET站点

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

如何把PHP站点用户登录会话传递到ASP.NET站点

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Version

Date

Creator

Description

<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chsdate w:st="on" isrocdate="False" islunardate="False" day="30" month="12" year="1899"><span lang="EN-US" style="FONT-FAMILY: Arial">1.0.0</span></chsdate>.1

<chsdate w:st="on" isrocdate="False" islunardate="False" day="21" month="2" year="2005"><span lang="EN-US" style="FONT-FAMILY: Arial">2005-2-21</span></chsdate>

郑昀@掌上灵通

草稿

目的:

用户从PHP制作的Web站点登录后,有些时候要去浏览另一个由ASP.NET制作的站点,但还要用当前的登录信息。

我们在PHP登录时,把登录信息等存储在了$_SESSION中,由于PHPSession是自己实现的,所以无法传递给ASP.NET

那么如何让ASP.NET站点知道用户已经登录了呢,从而把PHP登录会话传递给ASP.NET呢,而且拿到登录的一些参数呢?

当然我们可以直接传递参数,或者把Session存储在数据库中,但是前者不安全,我希望这些事情在后台完成,后者则有点麻烦。

那么有没有什么简单的办法呢?

解决:

基本原理是:

ASP.NET页面在Page_Load方法中,

首先查看请求本站点的RequestCookies,找到PHPSESSID
这就是标示当前访问者的SessionID

然后我们构造HttpWebRequest,以这个PhpSessionIDPHP站点下的CheckLogin.php页面请求,

看当前这个SessionID所代表的会话是否已经登录。

如果登录成功,那么该页面将返回我们规定的一些数值,由ASP.NET鉴别即可。

更多详情:

第一步,得到当前请求的Web页面的绝对路径:

String strAbsolutePath;

strAbsolutePath = Request.Url.AbsoluteUri;

int nPos = strAbsolutePath.LastIndexOf("/");

int nRemoveLength = strAbsolutePath.Length - nPos;

string strUriPath = strAbsolutePath.Remove(nPos, nRemoveLength);

第二步,遍历Cookie,寻找PHPSessionID

string strPHPSessionID = "";

for(int i=0;i

{

if("PHPSESSID" == Request.Cookies[i].Name)

{

strPHPSessionID = "PHPSESSID=" + Request.Cookies[i].Value;

}

}

第三步,通过ValidateLogin模拟HttpWebRequest带着PhpSessionID请求验证页面,看当前用户是否登录:

ValidateLogin vlLogin = new ValidateLogin(

strUriPath,

"/../MainSite/CheckLogin.php",

strPHPSessionID);

m_strLoginInfo = vlLogin.LoginInfo.Trim();

我们给System.Web.HttpWebRequest 对象创建一个新的CookieContainer,把PHPSessionID放进去,如下所示:

CookieContainer cookieCon = new CookieContainer();

hwrRequest.CookieContainer = cookieCon;

hwrRequest.CookieContainer.SetCookies(new Uri(strValidatePageURL),

m_strPHPSessionID);

m_strPHPSessionID是这样构造的:

PHPSESSID=.....

这样,ASP.NET发起的请求被主站点的PHP页面接收到之后,就会认为是同一个登录会话。

ValidateLogin.cs的代码如附录所示:

using System;

using System.Web;

using System.IO;

using System.Net;

using System.Text;

namespace Linktone.MySite.Components

{

/// <summary><p></p></summary>

/// ValidateLogin 的摘要说明:由于要集成到Php制作的MainSite站点中,

/// 首要就是登录统一。

/// 那么我们用这类来保证浏览本站点的用户已经先登录了MainSite站点;

/// 我们的基本原理是:

/// 首先查看请求本站点的RequestCookies,找到PHPSESSID,这就是标示当前访问者的SessionID

/// 然后我们构造HttpRequest,以这个PhpSessionIDMainSite站点下的CheckLogin.php请求,

/// 看当前这个SessionID所代表的会话是否已经登录;

/// 如果登录成功,那么该页面将返回一个字符串:“blablabla.

///

///

/// 作者:郑昀@掌上灵通 20050221

public class ValidateLogin

{

/// 要请求的验证页面的相对路径

private string m_strValidatePageRelateURL;

/// 从请求端传递过来的PHPSessionID

private string m_strPHPSessionID;

protected string m_strLastError;

private WebResponse m_webResponse;

public string ValidatePage

{

get { return m_strValidatePageRelateURL; }

set { m_strValidatePageRelateURL = value; }

}

public string LoginOperatorID

{

get { return m_strLoginOperatorID; }

set { m_strLoginOperatorID = value; }

}

/// <summary><p></p></summary>

///

///

/// 当前Web页面请求的绝对URL

/// 要请求的验证页面的相对路径

public ValidateLogin(string AbsoluteUri,

string ValidatePage,

string PHPSessionID)

{

m_strValidatePageRelateURL = ValidatePage;

m_strPHPSessionID = PHPSessionID;

/// 请求页面

RequestGetOperatorID(AbsoluteUri);

}

~ValidateLogin()

{

m_webResponse.Close();

}

public void RequestGetOperatorID(string strAbsoluteUri)

{

try

{

String Path;

Path = strAbsoluteUri;

/// 构建要访问的绝对路径

string strValidatePageURL = Path + m_strValidatePageRelateURL;

/// 构建请求的HttpWebRequest对象,并设置好Session

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

strValidatePageURL);

hwrRequest.Method = "GET";

hwrRequest.Proxy = System.Net.WebProxy.GetDefaultProxy();

// allow auto redirects from redirect headers

hwrRequest.AllowAutoRedirect=true;

// 30 second timeout for request

hwrRequest.Timeout=(int) new TimeSpan(0,0,60).TotalMilliseconds;

// give the crawler a name.

hwrRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

hwrRequest.ContentType = "application/x-www-form-urlencoded";

CookieContainer cookieCon = new CookieContainer();

hwrRequest.CookieContainer = cookieCon;

hwrRequest.CookieContainer.SetCookies(new Uri(strValidatePageURL),

m_strPHPSessionID);

hwrRequest.KeepAlive = false;

hwrRequest.ProtocolVersion = HttpVersion.Version10;

/// WebRequest 实例上的 GetResponse 方法

/// 将来自客户端应用程序的请求发送到在 URI 中标识的服务器。

m_webResponse = hwrRequest.GetResponse();

/// GetResponse EndGetResponse 方法返回一个 WebResponse 实例,

/// 该实例提供对服务器返回的数据的访问。

/// 因为此数据由 GetResponseStream 方法作为流提供给发出请求的应用程序,

/// 所以它可以在应用程序中的使用数据流的任何地方使用。

StreamReader sr = new StreamReader(

m_webResponse.GetResponseStream(),

Encoding.GetEncoding("GB2312"));

string sValidate = sr.ReadToEnd();

sr.Close();

/// 检验验证登陆的页面是否确认登陆了,否则直接引导到login.php页面

/// 调用者负责重定向

/// m_strLoginOperatorID = blabla;

}

catch (Exception eExcep)

{

m_strLastError = eExcep.Message;

}

return;

}

}

}

编写者:郑昀@linktone

Disclaimers

Programmer’s Blog List

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 0.75pt; HEIGHT: 0.75pt" type="#_x0000_t75" alt=""></shape>

博客堂

博客园

Don Box's Blog

Eric.Weblog()

Blogs@asp.net

本文档仅供参考。本文档所包含的信息代表了在发布之日,zhengyun_ustc对所讨论问题的当前看法,zhengyun_ustc不保证所给信息在发布之日以后的准确性。

用户应清楚本文档的准确性及其使用可能带来的全部风险。可以复制和传播本文档,但须遵守以下条款:

  1. 复制时不得修改原文,复制内容须包含所有页

  2. 所有副本均须含有 zhengyun_ustc的版权声明以及所提供的其它声明

  3. 不得以赢利为目的对本文档进行传播



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=296379


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值