views:

77

answers:

2

Hi

I am wondering how do I make pages automatically use https? Like if a user types in

http://www.mysite.com

It should take them right to the login page. However I have SSL required on this page(when they try to login).

So how could I make it so it would change it to

https://www.mysite.com even if they don't type it in themselfs?

+3  A: 

You can use the RequireHttpsAttribute on the appropriate controllers and/or actions:

[RequireHttps]
public class SecureController : Controller
{
    public ActionResult YourAction()
    {
        // ...
    }
}

// ...

public class YourController : Controller
{
    [RequireHttps]
    public ActionResult SecureAction()
    {
        // ...
    }
}
LukeH
Yes I do this. However if the user does not have https: in their address bar it will crash saying they are not using ssl. I want it so when they come that page it forces the url to have https.
chobo2
the above should work, knowing you have created successfully a signed certificate for your site, and if you are not using a debug mode (hit f5)
rob waminal
They did not do a very god job on this RequireHttps as it does not take in account for localhost nor does it change the request to https. It leaves it up to the user to type in https what I would not expect any user have to do. In the end I did something like thishttp://weblogs.asp.net/cibrax/archive/2009/01/19/running-a-partial-ssl-website-in-asp-net-mvc.aspx
chobo2
+1  A: 

i believe you are looking for

 [RequireSsl(Redirect = true)] 

there is a discussion you can find here

http://stackoverflow.com/questions/156748/ssl-pages-under-asp-net-mvc

Edited: found this link might be useful

http://blog.stevensanderson.com/2008/08/05/adding-httpsssl-support-to-aspnet-mvc-routing/

Sam Quest
The `RequireSsl` attribute was renamed `RequireHttps` in the release version of MVC2.
LukeH
Marked as the answer as links contained the answer I was looking for what was basically this. http://weblogs.asp.net/cibrax/archive/2009/01/19/running-a-partial-ssl-website-in-asp-net-mvc.aspx
chobo2