Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:11:53+00:00 2026-06-12T14:11:53+00:00

I am developing an ASP.Net MVC 3 Web Application. I need to have my

  • 0

I am developing an ASP.Net MVC 3 Web Application. I need to have my website secured with an SSL certificate, however, I only want this used when the application is on my live server, NOT on my test server.

Therefore, I setup an AppSetting in my Web Config like so

<appSettings>
    <add key="SSL" value="false" />
</appSettings>

Then in my Account Controller I get this value (either True or False) and using the value, decide whether or not to set the RequiresHttps attribute on my LogOn Action. I would like to do something like so

public class AccountController : Controller
{
        public string SSL = System.Configuration.ConfigurationManager.AppSettings["SSL"];

        if (SSL.Equals("true"))
        {
            [RequireHttps]
        }
        public ActionResult LogOn()
        {
            return View();
        }
}

But I know I can’t put my IF statement where it currently is, however, hopefully you get the idea of what I am trying to achieve.

Does anyone have any suggestions as to how I can implement my idea?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:11:55+00:00Added an answer on June 12, 2026 at 2:11 pm

    Subclass the RequireHttpAttribute (note this code is changed from my original answer – this new version will be more efficient):

    public class RequireHttpsIfEnabledAttribute : RequireHttpsAttribute
    {
      //this setting can't be changed without a recycle, so get it once and cache it.
      private static readonly Lazy<bool> HttpsRequired = new Lazy<bool>(() => {
        //if the AppSettings["SSL"] returns null you raise an exception if you do a
        //.Equals on it - so do it on the constant instead.  And make sure it's case
        //insensitive!
        return "true".Equals(System.Configuration.ConfigurationManager.AppSettings["SSL"],
          StringComparison.OrdinalIgnoreCase);
      });
      public override void OnAuthorization(AuthorizationContext filterContext)
      {
        //calling the base will fire the HTTPS check.  Not calling it will allow
        //non-SSL requests through
        if (HttpsRequired.Value)  
          base.OnAuthorization(filterContext);
      }
    }
    

    Now you just decorate your controllers/actions as before – but with your new attribute:

    [RequireHttpsIfEnabled]
    public class AccountController : Controller 
    {
      //....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I am developing an ASP.Net MVC 3 Web application using Entity Framework 4.1, however,
I'm developing an web application using asp.net mvc, and i need to do a
I have an Asp.Net MVC Web Application that I am developing. I have TeamCity
For the web application (ASP.NET MVC) I'm currently developing, we have the following architecture
I am developing an ASP.Net MVC 3 web application. I have a ViewModel public
I am developing an ASP.Net MVC 3 Web Application. I have a Razor View
I'm developing an web application using asp.net MVC and jQuery. I have in my
I have just started developing a full-web application by using the ASP .NET MVC
I'm developing web application using ASP.NET MVC + WCF secured by WIF. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.