Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net (C#) How to POST to HTTPS from an HTTP page

Tags:

c#

asp.net

https

C# 3.0 ASP.Net 2.0 IIS6

I have a regular [non-https] page. There is the standard one ASP.Net form on the page.

There are two "areas" of functionality on the page though. Login and "Get Quote". The login page needs to POST to HTTPS while the rest of the page [including the "other area"] form can't be HTTPS. In Java [JSP] and regular Html, we would just have two forms. One that posts to HTTPS and one that doesn't.

What is the way to handle this in ASP.Net [from one page]. I know that I could link to an HTTPS login.aspx page, but the business really would like the context together.

Any ideas?

Thanks,

like image 647
Jason V Avatar asked Mar 06 '09 17:03

Jason V


People also ask

What is ASP.NET C?

ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services. The name stands for Active Server Pages Network Enabled Technologies. ASP.NET (software)

Is ASP.NET like C#?

Basically, ASP.NET is a web delivery mechanism that runs either C# or VB.NET in the background. C# is a programming language that runs ASP.NET as well as Winforms, WPF, and Silverlight. There isn't really a comparison here.

What is ASP.NET C# used for?

ASP.NET is a free web framework for building great websites and web applications using HTML, CSS, and JavaScript. You can also create Web APIs and use real-time technologies like Web Sockets.

Does .NET support C?

No. The . NET Framework is a bunch of classes(libraries) abstracting some lower-level windows functionality. C and C++ are languages.


2 Answers

The solution is to use asp.net to specify a "cross page postback", that is, you user the PostBackUrl property of any button control (LinkButton, Button, ImageButton etc.). This property allows you to post back to any page you like. Just set your PostBackUrl to the https version of your page and you're good to go (also make sure there are no url redirects active which force http on your page).

// ensure we send credentials over a secure connection
if (!HttpContext.Current.Request.IsSecureConnection)
{
     string postbackUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace("http", "https");
     LinkButton_Login.PostBackUrl = postbackUrl;
}

In your specific case you should set one of your buttons to post back to the https version, and the other to the http version (if you don't specify the PostBackUrl the default is to post back to the page itself as is).

like image 98
Torfi Avatar answered Oct 12 '22 16:10

Torfi


You can have two forms on an aspx page. You just can't nest them.

On a page I built, I have one form that posts back to the page, and one that posts back to Google Checkout.

If you have to mix the contents of the page, put the https form at the bottom of the page (after the main form tag) and fill it with hidden fields. When the user clicks a button, use Javascript to assign values to the hidden fields and then post the https form.

like image 38
Ryan Michela Avatar answered Oct 12 '22 14:10

Ryan Michela