Disable HTTP Access via web.config

asp.netiissslweb.config

I want my users access a specific asp.net virtual directory only via SSL. Is there an option in web.config that allows me to specify this?

Best Answer

You can do that in IIS7. Go to SSL settings for your application, you will see check box with option saying "Require SSL", check the check box and your job is done. Now your website can be accessed from https only and not from http. Remember you need to have SSL certificate otherwise browsers will show some warning messages for your website. And ya..I don't think you can achieve this with web.config.

Edit: sample code

Full customization is possible using Global.asax file. You can add specific conditions and apply https or http. Below sample code shows that if page is Login/checkout and if connection is not secure redirect from http to https and also I may not need https for contact page.

 protected void Application_BeginRequest(Object sender, EventArgs e)
    {           
            if ((Request.Path.EndsWith("login.aspx") || Request.Path.EndsWith("checkout.aspx") ) && !Request.IsSecureConnection)
            {
                Response.Redirect(Request.Url.AbsoluteUri.Replace("http:", "https:"));
            }
            else if (Request.IsSecureConnection && !Request.Path.EndsWith("contact.aspx"))              
            {
                Response.Redirect(Request.Url.AbsoluteUri.Replace("https:", "http:"));
            }
        }