Home » ASP.NET » ASP.NET Cross Domain Form Submission

ASP.NET Cross Domain Form Submission

G03A0021

Not to be confused with cross page posting, cross domain submission allows us to post the contents of an ASP.NET form to a completely different domain.

To achieve this we will need to use a bit of javascript and you’ll need to resort to using regular HTML controls.

We will cover two cases.  The first is pretty easy.  You just need a form on your screen that will allow you to send the information to another domain.  The second will be more complex.  You need a form on your screen that you fill from your domain prior to sending to the second domain.

To start, create a new ASPX page.  You can use a master page if you want.  It won’t make much difference.

Because ASP.NET has it’s own FORM tags, to add another form tag into the output, you’ll want to close off the current form tag and start another.  The best way to do this is with a literal control.  This way the ASP.NET runtime engine will not be confused and any of the other runat=server controls you have on the page after this new form will continue to behave as expected.  However, you’ll want to make sure that any controls that produce input tags of their own all appear prior to the new form tag.

<%-- It is best if all your pages controls exist 
    prior to the new form here --%>
<asp:Literal ID="Literal1" runat="server" Text=
    '</form><form name="subform" action="newDomainAndPage" method="POST">'
    ></asp:Literal>
 <input type="text" name="someName" />
 <input type="submit" name="submit" value="Submit" />

The submit button will now post to the site specified in “newDomainAndPage”

If all of the information on the page is going to go to the new domain, things get a little easier.  You won’t need this sub form.  But, you will need a small snippet of javascript to change the action attribute in the form tag that .NET creates for you.

<script type="text/javascript" language='javascript'>
document.remote.action=
    "https://someotherdomain.com/someotherpage";
</script>

where “remote” is the ID of the form in your form tag when it is rendered.  If you are using a master page, the form tag will be mangled.  You’ll either need to generate the javascript in your codebehind and grab the form tag’s ClientID or you’ll need to do a view source and see what it got changed to.  Using ClientID is preferred because the ID could change over time (say, when a new version of .NET comes out).

 

Other post in ASP.NET

Related Post

  • ASP.NET Response.Redirect() and JavaScriptASP.NET Response.Redirect() and JavaScript Yesterday we covered issues surrounding using ASP.NET's Response.Redirect in server side code. We noted that not handing it correctly could prevent code from running on the server that we […]
  • Why CSS ID selectors are Evil in ASP.NET Web FormsWhy CSS ID selectors are Evil in ASP.NET Web Forms Anyone familiar with CSS knows that class selectors are generally reserved for controlling how an element looks (font, color, size, etc) and id selectors are generally reserved for where […]
  • Multi-Step Processing in ASP.NETMulti-Step Processing in ASP.NET I received the following question a few days ago but I’ve been so busy with billable work that I just haven’t had a chance to answer it until now.  Actually, I’m still busy, but I […]
  • Dynamically Change class Attribute From ASP.NETDynamically Change class Attribute From ASP.NET I recently received a question from another programmer I know who's been using PHP prior to ASP.NET that made me think harder about a problem we've all had in ASP.NET.  The basic […]
  • Using Node.js and Gulp with ASP.NET in Visual Studio 2015Using Node.js and Gulp with ASP.NET in Visual Studio 2015 As I’ve written before, I’m using Angular a lot recently to write the client side of my web applications.  As I’ve gotten to the end of my current project, I found myself needing to […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback

  • Pingback: Dew Drop – November 12, 2009 | Alvin Ashcraft's Morning Dew()

  • Something about that feels really wrong… but I’m sure that it works great. However, I would probably just resubmit the form data in the code behind. I’m pretty sure it’s easy to do that.

    • Dave

      John,

      Sometimes you have some interesting, even potentially better, ways of doing things. But in this case…?

      Even if you resubmit, you’ll need to do something similar to one of the two ways I suggest here, or you’ll have to go to a whole lot more work to do it.