4

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?

chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

3

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
  • 263,068
  • 57
  • 365
  • 409
  • 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 Aug 18 '10 at 00:40
  • 1
    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 Aug 18 '10 at 00:50
  • 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 this http://weblogs.asp.net/cibrax/archive/2009/01/19/running-a-partial-ssl-website-in-asp-net-mvc.aspx – chobo2 Aug 19 '10 at 22:41
1

i believe you are looking for

 [RequireSsl(Redirect = true)] 

there is a discussion you can find here

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/

Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • The `RequireSsl` attribute was renamed `RequireHttps` in the release version of MVC2. – LukeH Aug 18 '10 at 00:56
  • 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 Aug 19 '10 at 22:41