简体   繁体   中英

How can i get the password of oracle user instance from oracle connection string using C#?

I want to get the user id and the password of oracle db instance from the connection string that i have stored in my App.config file. Here is the connection string stored in App.Config File

<add name="MyConnection" connectionString="Data Source=xe;User ID=UsmanDBA;Password=root;"   providerName="System.Data.SqlClient" />

I have tried OracleConnectionString Builder but it does not return the password of connection string Here is the code:

 public string ConPass()
        {
            OracleConnectionStringBuilder builder = new OracleConnectionStringBuilder();
            builder.ConnectionString = con.ConnectionString;
            return builder.Password;
        }

this method does return the user id but not the password Is there something i am missing? or is there any other way to do this? Kindly help me to sort this out..

ConnectionString property never contains password. That is the security measure. In your code, password has been lost in this line:

builder.ConnectionString = con.ConnectionString;

You have to devise a different approach. For example, to read the connection string from config and then to feed it to the connection string builder. This might not be generally applicable if you only have the connection and no information from which config entry it was constructed...

On a related note, SQL Server connection (SqlConnection) exposes the Credential property which could be used to read password (I haven't actually tried this). I don't know of similar property in Oracle connection implementation.

You can make use of the SqlConnectionStringBuilder

string conString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
OracleConnectionStringBuilder builder = new  OracleConnectionStringBuilder(conString);
string user = builder.UserID;
string pass = builder.Password;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM