views:

228

answers:

5

Hello All,

I have a page www.senderdomain.com/sender.aspx, from which i need to write a string to another page in other domain www.receiverdomain.com/receiver.aspx

In sender.aspx i have written

Response.Write("Hello");
Response.Redirect(Request.UrlReferrer.ToString());

It gets redirected to respective receiver.aspx page, but I am not sure how to get the text "Hello" in receiver.aspx page. Can any pl help on this?

+1  A: 

pass data in query string because can not do like this

for example

   Response.Redirect(Request.UrlReferrer.ToString() + "?mytext=hello");

And in receiver page access querystring data, will resolve your issue.

use private algorithm like 

 string message = "hello";
 add 1 to each char so that hello become ifmmp

and on receiver side -1 from each char so it will be hello 
Pranay Rana
The problem here will be the security issue. I need to pass the information in secured manner and i can't encrypt the text as the receiver will not know to decrypt it
Sri Kumar
create you private encrypt algorithm and use that same on other side to decrypt it for exampl . and one to each char in your string
Pranay Rana
A: 

You need to pass the value in the url or post it in a cross page postback.

For secure cross domain communication, take a look at SAML (Security Assertion Markup Language). It is a standard way of passing information securely across domain boundaries. It is most often used in Single Sign On scenarios, but it can be used to pass data securely. Are you using certificates? What type of encryption are you using?

Another option would be to save state to a database or filesystem that is accessible to both domains.

Daniel Dyson
But here the session are going to be different as domain varies
Sri Kumar
A: 

The Response.Redirect method will scrap everything that you have written to the page, and replace it with a redirection page, so you can't send any content along with the redirect.

The only option to send data along in a redirect (that works between differnt domains and different servers) is to put it in the URL itself. Example:

string message = "Hello";
Response.Redirect(Request.UrlReferrer.ToString() + "?msg=" + Server.UrlEncode(message));

Another option is to output a page containing a form that is automatically posted to the destination:

string message = "Hello";
Response.Write(
  "<html>" +
  "<head><title>Redirect</title></head>" +
  "<body onload=\"document.forms[0].submit();\">" +
  "<form action=\"" + Server.HtmlEncode(Request.UrlReferrer.ToString()) + "\" method=\"post\">" +
  "<input type=\"hidden\" name=\"msg\" value=\"" + Server.HtmlEncode(message) + "\">" +
  "</form>" +
  "</body>" +
  "</html>"
);
Response.End();

You can use Request.Form["msg"] on the recieving page to get the value.

Guffa
A: 

Don't use the built-in URL encode, if you want to avoid all sorts of problems later.

    String UrlEncode(String value)
    {
        StringBuilder result = new StringBuilder();

        foreach (char symbol in value)
        {
            if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".IndexOf(symbol) != -1) result.Append(symbol);
            else result.Append("%u" + String.Format("{0:X4}", (int)symbol));
        }

        return result.ToString();
    }

The above supports unicode, and pretty much everything.

Theofanis Pantelides
+1  A: 

It seems you have a value on Sender.aspx that you need to display in receiver.aspx. This is how you can do it.

//On Page_Load of sender.aspx 

Session["fromSender"] = "Hello";
Respone.Redirect("receiver.aspx");
Response.End();

//On Page_Load of receiver.aspx 

if(!string.IsNullOrEmpty(Session["fromSender"].ToString()))
    Response.Write(Session["fromSender"].ToString());

EDIT

In case of change in domain, immediate easy way is to pass the value in query-string.

//On Page_Load of sender.aspx 

Response.Redirect("http://www.receiverdomain.com/receiver.aspx?fromSender=Hello");
Response.End();

//On Page_Load of receiver.aspx 

if(!string.IsNullOrEmpty(Request.QueryString["fromSender"].ToString()))
    Response.Write(Request.QueryString["fromSender"].ToString());

You may observe that the code pattern remains the same and container that is used to transfer the value changes from Session to QueryString.

EDIT2

If security is a concern with you in this case and you don't wish to expose the value ["Hello"], then here comes another way that can help you. In this solution we will first redirect the page to receiver and then from receiver it shall ask for the value to sender. So first we'll write the code for receiver.

//On Page_Load of receiver.aspx 
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Remember to use System.Net namespace
        HttpWebRequest requestToSender = (HttpWebRequest)WebRequest.Create("http://www.senderdomain.com/sender.aspx?cmd=getvalue");
        HttpWebResponse responseFromSender = (HttpWebResponse)requestToSender.GetResponse();
        string fromSender = string.Empty;

        //Remember to use System.IO namespace
        using (StreamReader responseReader = new StreamReader(responseFromSender.GetResponseStream()))
        {
            fromSender = responseReader.ReadToEnd();
        }
        Response.Write(fromSender);
        Response.End();
    }
}

And in the sender.aspx

//On Page_Load of sender.aspx 
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["cmd"].ToString()))
        {
            string cmd = Request.QueryString["cmd"].ToString();
            if (cmd.Equals("getvalue", StringComparison.OrdinalIgnoreCase))
            {
                Response.Clear();
                Response.Write("Hello");
                Response.End();
            }
        }

        Response.Redirect("http://www.receiverdomain.com/receiver.aspx");
        Response.End();
    }
}
this. __curious_geek
But here the domain differs then how could i get the session?
Sri Kumar
Great..! But here the receiver will POST files to the sender initially and upon receiving, the sender will send the acknowledgement. Is it possible to POST the files from the receiver to sender?
Sri Kumar
its the same case. sender can simply redirect to recvr and rcvr can get the value from sender via either http-get or http-post.
this. __curious_geek
Here I was able to get the value from the sender (i.e Hello) in the receiver. But I need to POST the receiver's form value initially to the sender. I use NameValueCollection postPageCollection = Request.Form; and HttpFileCollection postCollection = Request.Files; to receive Form and Files from receiver but i don't see any values
Sri Kumar
NameValueCollection postPageCollection = Request.Form;foreach (string name in postPageCollection.AllKeys){Response.Write(name + " " + postPageCollection[name]);}HttpFileCollection postCollection = Request.Files;foreach (string name in postCollection.AllKeys){HttpPostedFile aFile = postCollection[name];aFile.SaveAs(Server.MapPath(".") + "/" + Path.GetFileName(aFile.FileName));}Response.Clear();Response.Write("Success");Response.End();
Sri Kumar
I have the above code in my sender which should receive the receiver's form content and uploaded file. The code given here doesn't get the values from receiver
Sri Kumar