How to pass the PHP site user logon session to the ASP.net site

Source: Internet
Author: User
Tags php session relative
Asp.net| site

Objective:
After users log on from PHP's web site, they sometimes have to browse another site made by ASP.net, but also use current login information.

We login in PHP, the login information stored in the $_session, because the PHP session is its own implementation, so can not be passed to ASP.net.

So how to let ASP.net site know that the user has logged in, so that the PHP login session to ASP.net, and get some of the parameters of the login?

Of course we can pass parameters directly, or store the session in the database, but the former is not safe, I hope these things in the background, the latter is a bit of trouble.

So is there any easy way to do that?

Solve:
The rationale is:

Asp. NET page in the Page_Load method,

First look at the request for this site's cookies, find Phpsessid,
This is the SessionID that marks the current visitor;

Then we construct HttpWebRequest, with this phpsessionid to the checklogin.php page request under the PHP site,


See if the session currently represented by this SessionID is logged in.

If the login is successful, the page will return some of our specified values, which can be identified by ASP.net.


More details:
The first step is to get the absolute path of the currently requested Web page:

String Strabsolutepath;

Strabsolutepath = Request.Url.AbsoluteUri;

int NPOs = Strabsolutepath.lastindexof ("/");

int nremovelength = Strabsolutepath.length-npos;

String struripath = Strabsolutepath.remove (NPOs, nremovelength);

Step two, walk through cookies and look for Phpsessionid:

String Strphpsessionid = "";

for (int i=0;i < request.cookies.count;i++)

{

if ("phpsessid" = = Request.cookies[i]. Name)

{

Strphpsessionid = "phpsessid=" + request.cookies[i]. Value;

}

}

The third step, through Validatelogin simulation HttpWebRequest with Phpsessionid request to verify the page, to see whether the current user login:

Validatelogin vllogin = new Validatelogin (

Struripath,

"/.. /mainsite/checklogin.php ",

Strphpsessionid);

M_strlogininfo = VlLogin.LoginInfo.Trim ();

We create a new Cookiecontainer for the System.Web.HttpWebRequest object and put the Phpsessionid in as follows:

Cookiecontainer Cookiecon = new Cookiecontainer ();

Hwrrequest.cookiecontainer = Cookiecon;

HwrRequest.CookieContainer.SetCookies (New Uri (Strvalidatepageurl),

M_strphpsessionid);

M_strphpsessionid is constructed in this way:

Phpsessid= .....

In this way, ASP. NET is considered to be the same logon session after it is received by the PHP page of the primary site.

The ValidateLogin.cs code is shown in the Appendix:
Using System;

Using System.Web;

Using System.IO;

Using System.Net;

Using System.Text;

Namespace Linktone.MySite.Components

{

<summary>

Validatelogin's summary description: to integrate into the PHP-made mainsite site,

The first is to sign up for unification.

Then we use this class to ensure that users browsing the site has first logged into the Mainsite site;

Our rationale is:

First look at the request of this site requested cookies, find Phpsessid, which is to mark the current visitors SessionID;

Then we construct HttpRequest, with this phpsessionid to the checklogin.php request under the Mainsite site,

See if the session currently represented by this sessionid is logged in;

If the login succeeds, the page returns a string: "Blablabla".

</summary>

///

Author: Zheng @ Handheld 20050221

public class Validatelogin

{

The relative path of the validation page to request

private string M_strvalidatepagerelateurl;

Phpsessionid passed from the request side.

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>

///

</summary>

<param name= "Absoluteuri" > The absolute url</param> of the current Web page request

<param name= "Validatepage" > The relative path of the validation page to be requested </param>

Public Validatelogin (String Absoluteuri,

String Validatepage,

String Phpsessionid)

{

M_strvalidatepagerelateurl = Validatepage;

M_strphpsessionid = Phpsessionid;

Request page

Requestgetoperatorid (Absoluteuri);

}

~validatelogin ()

{

M_webresponse.close ();

}

public void Requestgetoperatorid (string strabsoluteuri)

{

Try

{

String Path;

Path = Strabsoluteuri;

Building an absolute path to access

String strvalidatepageurl = Path + M_strvalidatepagerelateurl;

Build the requested HttpWebRequest object and set the 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;

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;

The GetResponse method on WebRequest instance

Sends a request from a client application to the server identified in the URI.

M_webresponse = Hwrrequest.getresponse ();

The GetResponse and EndGetResponse methods return a WebResponse instance,

This instance provides access to the data returned by the server.

Because this data is provided by the GetResponseStream method as a stream to the requesting application,

So it can be used anywhere in the application where the data flow is used.

StreamReader sr = new StreamReader (

M_webresponse.getresponsestream (),

Encoding.GetEncoding ("GB2312"));

String svalidate = Sr. ReadToEnd ();

Sr. Close ();

Verify that the landing page is confirmed, or direct to the login.php page

The caller is responsible for redirection

M_strloginoperatorid = BlaBla;

}

catch (Exception eexcep)

{

M_strlasterror = Eexcep.message;

}

Return

}

}

}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.