I would like to know if it is possible to change the web.config file programmatically, specifically, to change authentication mode="" ? So that the user can choose, through the database, he can log in either through the active directory .

    2 answers 2

    Web.config is a regular xml file, and you can work with it as with a regular xml file. But! If you change it, you will need to restart the application, so it is not desirable to change something in it programmatically. To solve your problem, I would try to do this: as you know, web.config files can be created in each application folder. So you need to create two folders in them to create your own web.config and write them into different authentication modes. Also in folders, create an aspx page, in which the actual authentication will take place. I never did that, and I don’t know whether it will work or not. If you try to do so, then write it down, please, it works or not.

      So much I have never google =). But having achieved the result and received moral satisfaction, he was inspired to further work. Code for followers:

       using System.Web.Configuration; using System.Security.Principal; using System.Configuration; 

      System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); AuthenticationSection authenticationSection = (AuthenticationSection)configuration.GetSection("system.web/authentication"); authenticationSection.Mode = AuthenticationMode.Windows; //чтобы вернуть назад соответственно ставим .Forms configuration.Save(ConfigurationSaveMode.Minimal);

      • And what works? Is it possible without restarting the application? - DmitryBLR
      • one
        Yes, it works, invented for a long time, the only thing that is necessary after the execution of this code is to postback, so I decided to make a radiobutton with the choice of the type of authorization with a postback, and from the bottom another button with redirection to another page, and after clicking on the button the authentication changes and the user receives required result. - Mr_Lambert_13