Connecting to oracle database with proxy address - c#

So I'm trying to connect to an Oracle database using the Oracle.ManagedDataAccess-library, where I'm using the following datasource:
(DESCRIPTION =(SOURCE_ROUTE = YES)
(ADDRESS_LIST=
(ADDRESS = (PROTOCOL = TCP)(Host = PROXY-OracleConnectionManager)(Port = 1111))
(ADDRESS = (PROTOCOL = TCP) (Host = MAIN-DATABASE) (Port = 0000)))
(connect_data= (UR = A)(SERVICE_NAME = SERVICENAME)))
I'm also providing a user id and password.
We don't have any control over the database on our end, but as far as I know, they are using a connection manager as the first address, which should route us to the next address in the list if we are authenticated. This works when using this in Oracle SQL Developer but does not work programatically with Oracle.ManagedDataAccess.
This is how I build and use the connection string:
var connString = new OracleConnectionStringBuilder
{
{"User Id", settings.DbUserId},
{"Password", settings.DbUserPassword},
{"Data Source", settings.DbDataSource}
};
OracleConnection conn = new OracleConnection(connectionString.ToString())
conn.Open()
When I run this, I get the following error:
ORA-12537: Network Session: End of file
I suspect that the issue is the ADDRESS_LIST and that the routing doesn't work, but I can't say for sure. Anyone able to provide some insight?

Related

Convert Entity Framework DB Connection to use Azure Identity using Powershell Credentials

We have an application that is setup to use EntityFramework. I need to update that application to access the database using Azure Identity. Basically I need to do the following:
Get the access token using AzurePowershell Credentials
Create the SQL Connection using that access token
Add the token to the connection and return it to the caller
Here's what I have so far (but I know i'm missing alot of things here coz I am getting compilation errors). I was looking for some examples to do this but wasn't successful.
public static DbConnection CreateConnection( string efConnectionString )
{
var credential = new AzurePowerShellCredential();
System.Threading.CancellationToken cancellationToken = default;
TokenRequestContext requestContext = "https://database.windows.net/.default";
string accessToken = AzurePowerShellCredential.GetToken( "https://database.windows.net/.default", cancellationToken );
SqlConnectionStringBuilder sqlConnection = new SqlConnectionStringBuilder( efConnectionString );
//create sql connection
using( SqlConnection sqlConn = new SqlConnection( efConnectionString ) )
{
sqlConn.AccessToken = accessToken;
return sqlConn;
}
For starters the requestContext doesn't like being set up as a string and I'm trying to figure out the best way to add the token to the connection string and send it back successfully. Also, the AzurePowerShellCredential.GetToken doesn't like the string that I am passing with the database address.
My Connection string that is coming in looks like this:
"Server=tcp:servername.database.windows.net,1433;Database=databasename;User ID=UserID#servername;Password=password;Trusted_Connection=False;Encrypt=True;"
In order for this to work successfully, I will be ommitting the userid and password from the above connections string so it can be replaced by the AzureCredentials.
I really appreciate any help that can be provided. Thanks!
Your code is correct. The only thing I would suggest is to use DefaultAzureCredential which would allow you to use different authentication flows for the database.
As for a connection string it's format can be following:
using Azure.Core;
using Azure.Identity;
using System.Data.SqlClient;
var connString = "Server=tcp:<your-server-name>.database.windows.net,1433;Database=<database-name>;";
var credential = new DefaultAzureCredential();
var token = credential
.GetToken(
new TokenRequestContext(scopes: new[] { "https://database.windows.net/.default" })
);
using var conn = new SqlConnection(connString);
conn.AccessToken = token.Token;
conn.Open();
One more thing you should think about is access token renewal. EntityFramework caches database connections so the token might expire while the connection is alive. As an option I can suggest you to use existing solution like in this example if you have an ASP.NET app or try to reverse-engineer token update logic like in Microsoft.Data.SqlClient (example)

Query services remotely using WMI

I want to get status of services on remote server (Windows 2008 R2). I am using domain account. My problem is that instead of 150+ services, I am getting only 34.
Here is my code:
var connection = new ConnectionOptions
{
Username = $"{domain}\\{userName}",
Password = password,
};
var scope = new ManagementScope(#"\\{ip}\root\cimv2", connection);
var query = new ObjectQuery(importConfig.Query);
var searcher = new ManagementObjectSearcher(scope, "SELECT * FROM Win32_Service");
var result = searcher.Get()
When I add my domain account do Administrators on this server, I am getting valid response - 150+ services.
I also tried to set (I tried AU, SID and many other combinations):
sc sdset SCMANAGER D:(A;;CCLCRPRC;;;AU)(A;;CCLCRPWPRC;;;SY)(A;;KA;;;BA)S:(AU;FA;KA;;;WD)(AU;OIIOFA;GA;;;WD)
but the result is the same - 34 services are returned when domain account is not in Admins. Any idea why I don't see other services?

Connection to a query router (mongos) from C# and list databases

I nee to connect to a mongo and run a commands.
I'm am connecting using the following piece of code. I want to test weather I am connecting by listing the databases.
MongoClient client = new MongoClient("mongodb://127.0.0.1:27017");
client.ListDatabases();
If I debug and click on the second line I cannot see the names of the databases. How can I print the names of the databases to screen to confirm I am connected to mongo.
You need to specify credentials in the Connection String. Couple ways you can do this:
var connectionString = "mongodb://user1:password1#127.0.0.1:27017";
Is the format expected, you will have to supply the username and password yourself, these are just placeholders.
Or you can create a MongoCredentials object and use that instead of a connection string (probably a bit cleaner this way, and allows more configuration if you look deeper into the object documentation)
var credential = MongoCredential.CreateMongoCRCredential("test", "user1", "password1");
var settings = new MongoClientSettings
{
Credentials = new[] { credential }
};
var mongoClient = new MongoClient(settings);
Both of these examples are found on MongoDB's documentation site
Try GetDatabaseNames() method and also assign the result to a variable. So that you can inspect it at breakpoint like
MongoClient client = new MongoClient("mongodb://127.0.0.1:27017/");
var databaseNames = client.GetDatabaseNames();
ListDatabases returns an IAsyncCursor so try the following:
var client = new MongoClient(<CONNECTION STRING>);
var cursor = client.ListDatabases();
cursor.ForEachAsync(db => Console.WriteLine(((BsonString)db["name"]).Value));

Unable to connect to RabbitMQ on server

I have the below code that connects to RabbitMQ on my local machine but when I change Host name from localhost to my servername it fails and returns the error
var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "localhost";
As soon as I change HostName as below, it returns error
factory.HostName = "myserver";
Exception: None of the specified endpoints were reachable
The AMQP operation was interrupted: AMQP close-reason, initiated by
Library, code=0, text=\"End of stream\", classId=0, methodId=0,
cause=System.IO.EndOfStreamException: Peer missed 2 heartbeats with
heartbeat timeout set to 60 seconds
Instead of connecting this way, it's much easier to connect using a connection string like you would with sql.
Example C#:
var factory = new ConnectionFactory
{
Uri = ConfigurationManager.ConnectionStrings["RabbitMQ"].ConnectionString,
RequestedHeartbeat = 15,
//every N seconds the server will send a heartbeat. If the connection does not receive a heartbeat within
//N*2 then the connection is considered dead.
//suggested from http://public.hudl.com/bits/archives/2013/11/11/c-rabbitmq-happy-servers/
AutomaticRecoveryEnabled = true
};
return factory.CreateConnection();
web.config or app.config
<connectionStrings>
<add name="RabbitMQ" connectionString="amqp://{username}:{password}#{servername}/{vhost}" />
</connectionStrings>
On server, it looks like Host Name is configured different. My Admin looked at logs and looked at configuration and provided me with the Host Name on server.
var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/filestream";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "myserver";

MySQL Connection timing out C#

My Code is below:
MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
conn_string.Server = "mysql.***********.com";
conn_string.UserID = "********";
conn_string.Password = "********";
conn_string.Port = 3306;
conn_string.Database = "*********";
var connectionString = conn_string.ToString();
var connection = new MySqlConnection(connectionString);
connection.Open();
The error that I get is System.TimeoutException
Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
The code worked before when I was on a different network, but now that I have changed networks it no longer works and I get a timeout error on the connection.Open(); command.
Any ideas on what is wrong? I know that the database/server and even the computer I'm using has not changed but my network has.

Categories

Resources