2

Basically the application I am working on has a bunch of parameters that are all controlled from an xml config file, which contains configurations for each machine that runs the applcation (live and dev). The problem I have is, I want to be able to control the forms based authentication requiressl field, so that in the configuration file, I can specify this flag.

This should cause the requiressl flag to be true in the live environment, and there by enforce ssl and create a secured auth cookie aswell. For the dev environment, I do not wish to enforce ssl on all the dev machines.

I do not want to manipulate the web.config file to do this. I would like my application code to read in the xml configuration and set them accordingly.

I have tried using reflector to go into the sealed formsauthentication class, and have found that the requiressl property only has a get method, no set method.

I have also tried to implement the formsauthenticationconfiguration class, to no avail. - I am not sure that I am doing this correctly.

Any help in finding a possible solution to this would be greatly appreciated.

Kind regards,

Gurpreet

Attempt 1:

Subsequently I have tried this :

PropertyInfo field = typeof (FormsAuthentication).GetProperty("RequireSSL");
field.SetValue(typeof(FormsAuthentication), true, null)

I get an error message saying "Property set method not found"

Attempt 2: SOLVED

typeof (FormsAuthentication).GetField("_RequireSSL", BindingFlags.Static | BindingFlags.NonPublic).SetValue(typeof(FormsAuthentication), Config.Secure);

swissarmykirpan
  • 452
  • 5
  • 14
  • What are your reasons for not wanting to modify the web.config file? One of the main purposes of that file is to configure it for specific environments, which is exactly what it sounds like you are trying to do. – Brian Ball Dec 14 '10 at 13:59

1 Answers1

1

Although not recommended, You can use reflection to modify a private field (the backing store for the property).

Try here

Community
  • 1
  • 1
StingyJack
  • 19,041
  • 10
  • 63
  • 122