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 6612355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:05:18+00:00 2026-05-25T20:05:18+00:00

I have a connection string in web.config which custom-encrypted. I would like to decrypt

  • 0

I have a connection string in web.config which custom-encrypted.

I would like to decrypt this during application start (the first page is Login page which is based on a Master page. The login credentials are verified using the encrypted connection string) and it must be encrypted before application closes – by whatever way – either normal close or application error.

I tried to implement using Global.asax but since any changes to web.config restarts application, it went into a loop and hence gave up this method.

Please note that I do not want the default configuration encryption provided by ASP.NET as I use a custom one.

While it is easy to decrypt the connection string during startup, is there really any way to encrypt again during application close?

Many 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-05-25T20:05:19+00:00Added an answer on May 25, 2026 at 8:05 pm

    I am going to risk this as an answer because I can’t really see the need for what you describe:

    1. If the connection string is already encrypted in the web.config *_you_don’t_need_to_decrypt_it* when the application starts, you just decrypt it every time you instantiate a database connection. Believe me, the performance of decrypting the connection string is negligible even if you do it every time you open a connection. But assuming you are a performance freak and you only want to decrypt it once and put in Session (bad idea, but it appears that that’s what you are doing), there’s nothing to worry about as I will explain in point 3 below.

    2. Supposing that you decrypt it once (Application_Start, what have you), why do you say that you need to encrypt it again before application closes - by whatever way - either normal close or application error.? The connection string is not transferred over the wire, it’s something that it’s used on the server side in order to instantiate a connection to the database but it is not something that someone can see by using the application, unless of course, you store it in ViewState but that would be very silly.

    3. You mentioned that you store something in Session although is not 100% clear whether you are referring to the connection string or something else. Assuming it is the connection string (again, I can’t think of a valid reason for this. I apologize if there’s one.) it’s not something that any user can see since Session is nothing but memory bytes on the server. The same applies for Cache.

    So, that’s that.

    You decrypt the connection string, instantiate your connection, do your thing and close the connection. The connection string can stay encrypted in web.config for ever; untouched.

    UPDATE

    Since the OP is using the Membership provider, the solution is to implement your own Membership provider. You can download a sample project demonstrating how to do this from Microsoft at the following link: http://download.microsoft.com/download/a/b/3/ab3c284b-dc9a-473d-b7e3-33bacfcc8e98/ProviderToolkitSamples.msi

    Look at the SQLConnectionHelper.cs class.

    Here’s another post doing pretty much exactly what you need.

    UPDATE 2

    Here’s another way to do the same thing using Reflection. Call it a hack, but it seems to do the job:

    Inside Application_PreRequestHandler in Global.asax call this method, where connectionString is your connection string already decrypted:

    private void SetProviderConnectionString(string connectionString)
    {
    // Set private property of Membership, Role and Profile providers. Do not try this at home!!
    var connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                if (connectionStringField != null)
                    connectionStringField.SetValue(Membership.Provider, connectionString);
    
                var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                if (roleField != null)
                    roleField.SetValue(Roles.Provider, connectionString);
    
                var profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                if (profileField != null)
                    profileField.SetValue(ProfileManager.Provider, connectionString);
    }
    

    Source.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my applications web.config file I have a connection string stored. I encrypted it
I have a ASP.Net Web Application which connects to a SQL Server. The web.config
I have been tasked with securing the connection string in an classic ASP application
If I am encrypting the connection string section, anyone who has the web.config can
I would like to create a service which returns a simple string. So far
We have an MVC web which is running in Autofac. All the config is
I have just joined a project that has a connection string which is defined
How to rewrite Webconfig connection string at runtime.I have input textbox for Server,UserName and
I have a connection string being passed to a function, and I need to
I have the following connection string: Data Source=localhost\\SQLEXPRESS; , it complains that '' is

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.