How to use ip-address with username in connection string? - c#

I wrote this method, but it throws System.ArgumentException if "uid" has ' symbols in.
public void Init(string constr)
{
var server = "a222068_6.mysql.mchost.ru";
var database = "'a222068_6'";
var uid = "'a222068_6'#'10.0.2.13'";
var pass = "pass";
constr = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + uid + ";PASSWORD=" + pass + ";SSL Mode=None;";
cnt = new MySqlConnection(constr); // Exception is thrown here
}
Exception message on attempt to connect without '
Authentication to host 'a222068_6.mysql.mchost.ru' for user 'a222068_6#10.0.2.13' using method 'mysql_native_password' failed with message: Access denied for user 'a222068_6#10.0.2.13'#'localhost' (using password: YES)

Change
var uid = "'a222068_6'#'10.0.2.13'";
to
var uid = #"a222068_6#10.0.2.13";

Use a verbatim string literal:
Verbatim string literals start with # and are also enclosed in double quotation marks.
And there are several other StackOverflow answers with examples of how to use MySqlConnectionStringBuilder, such as this one:
var connectionStringBuilder = new MySqlConnectionStringBuilder
{
Server = "<instanceIp>",
UserID = "<userId>",
Password = "<password>",
Database = "<databaseName>",
CertificateFile = #"<Path_To_The_File>\client.pfx",
CertificatePassword = "<Password_For_The_Cert>"
};
var conn = new MySqlConnection(connectionStringBuilder.ToString())

Related

Creating and connecting to a new database

My application successfully creates a new database utilising the following string:
SqlString = $"CREATE DATABASE {_databaseName} ON PRIMARY " +
$"(NAME = {_databaseName}_Data, " +
$"FILENAME = '{_filePath}', " +
"SIZE = 8192KB, MAXSIZE = UNLIMITED, FILEGROWTH = 10%) " +
$"LOG ON (NAME = {_databaseName}_Log, " +
$"FILENAME = '{_databasePathLdf}', " +
"SIZE = 8192KB, " +
"MAXSIZE = UNLIMITED, " +
"FILEGROWTH = 10%)";
ExecuteSqlQuery(#"server=.\SQLEXPRESS; Trusted_Connection = Yes", SqlString);
When I then try to reconnect to the database use the following connection string
internal string ConnectionString(String databaseName)
{
return $"Server =.\\SQLEXPRESS;Database={databaseName};Trusted_Connection=Yes;";
}
I get the following error
For clarity the connection strings both use Trusted_Connection = Yes and the owner and login are the same when creating and connecting to the database.
For further clarity I am utilising OpenFileDialog to navigate to the database to connect utilising the following:
internal string OpenFileDialog(String initialDirectory)
{
string filePath = "";
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = initialDirectory;
openFileDialog.Filter = "mdf Database file (*.mdf)|*.mdf";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filePath = openFileDialog.FileName;
}
}
return filePath;
}
The error occurs on the DialogResult.OK.
How can you utilise OpenFileDialog in this instance to return the file path of the database to connect to?
I am using SQL Server Express 2019 (v15.0.2070).
Thanks in advance for any assistance.

Dropbox API - UploadAsync Value Should Match

I'm trying to use the Dropbox API in order to do a file upload.
string accesstoken = "token";
using (DropboxClient client =
new DropboxClient(accesstoken, new DropboxClientConfig("NameOfApp")))
{
string[] spitInputFileName = file.FileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
string filenameAndExtension = spitInputFileName[spitInputFileName.Length - 1];
string[] filenameAndExtensionSplit = filenameAndExtension.Split('.');
string originalFileName = filenameAndExtensionSplit[0];
string originalExtension = filenameAndExtensionSplit[1];
String filename = "" + originalFileName + Guid.NewGuid().ToString().Replace("-", "") + "." + originalExtension;
var updated = client.Files.UploadAsync(
filename,
mode: WriteMode.Overwrite.Overwrite.Instance,
body: file.InputStream).Result;
var result = client.Sharing.CreateSharedLinkWithSettingsAsync(filename).Result;
Within the body: file.InputStream.Result; section, It encounters below error :
An exception of type 'System.ArgumentOutOfRangeException' occurred in Dropbox.Api.dll but was not handled in user code
Value should match pattern '\A(?:(/(.|[\r\n])*)|(ns:[0-9]+(/.*)?)|(id:.*))\z'
What is the reason of this exception?
Paths should start with '/'.
File path should be something like this:
/test.txt
In your case:
String filename = "/" + originalFileName + Guid.NewGuid().ToString().Replace("-", "") + "." + originalExtension;

Creating a hash in Realex payments

I need to update the Expiry Date and update the Cardholder Name on an existing card in Realex payments.
The hash value syntax should be in the following format:
Timestamp.merchantID.payerref.ref.expirydate.cardnumber
And here is an example of how it should look
20030516175919.yourmerchantid.mypayer.card01.1015.
When I run the following method I get the error:
"sha1hash incorrect - check your code and the Developers Documentation"
private string ReturnHash(string timeStamp, string merchantId, string payerRef, string reference, string expDate, string cardNum )
{
SHA1 hash = new SHA1Managed();
StringBuilder builder = new StringBuilder();
builder.Append(timeStamp).Append(".");
builder.Append(merchantId).Append(".");
builder.Append(payerRef).Append(".");
builder.Append(reference).Append(".");
builder.Append(expDate).Append(".");
builder.Append(cardNum );
string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));
return resultingHash;
}
What am I doing wrong?
Thank you for your message.
Could you try before running this line of code:
string resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
To make "resultingHash" all lowercase?
Also before running:
resultingHash = BitConverter.ToString(hash.ComputeHash(Encoding.UTF8.GetBytes(resultingHash)));
make "resultingHash" to lowercase as well.
Thanks,
Borja
var timeStamp = RealexDateFormatter.DateFormatForRealex();
var orderid = model.ORDER_ID;
var secret = ConfigurationManager.AppSettings["Appsecretkey"];
var merchantId = ConfigurationManager.AppSettings["AppMerchantId"];
var temp1 = FormsAuthentication.HashPasswordForStoringInConfigFile(
timeStamp + "." +
merchantId + "." +
orderid + "." +
model.AMOUNT + "." + "EUR", "sha1");
temp1 = temp1.ToLower();
var temp2 = temp1 + "." + secret;
var sha1hash = FormsAuthentication.HashPasswordForStoringInConfigFile(temp2, "sha1");
sha1hash = sha1hash.ToLower();`enter code here`
var url = "https://hpp.sandbox.realexpayments.com/pay?MERCHANT_ID="
+ ConfigurationManager.AppSettings["AppMerchantId"] +
"&ORDER_ID=" + orderid + "&CURRENCY=EUR" + "&AMOUNT=" + model.AMOUNT + "&TIMESTAMP=" + timeStamp + "&SHA1HASH=" + sha1hash + "&MERCHANT_RESPONSE_URL=http://deposit.projectstatus.in/Payment/Response";

C# LDAP against LDS

I'm trying to resolve this problem but I cannot get this to work.
The Question
What is wrong with my query?
The Code
private static void ExecuteQuery()
{
string sDomain = "10.12.14.165:389";
string sDefaultOU = "CN=GeneralUsers,CN=Company,DC=Server,DC=LDS,DC=LOCAL";
string sServiceUser = "myUser";
string sServicePassword = "myPassword";
DirectoryEntry ldapEntry = new DirectoryEntry("LDAP://" + sDomain + #"/" + sDefaultOU,
sServiceUser,
sServicePassword);
DirectorySearcher ldapSearcher = new DirectorySearcher(ldapEntry);
//Error Occurs here
SearchResultCollection ldapResult = ldapSearcher.FindAll();
}
The Error
# SearchResultCollection ldapResult = ldapSearcher.FindAll();
Exception: There is no such object on the server.
Attention
The Object DOES Exist
The problem occured in the LDS Configuration. the user I was authenticating with had no rights to the LDS, the query I was using was working fine.

Using VSPE tcpclient/tcpserver in C#

I tried to create TcpClients and TcpServers in my C# program using VSPE COM service. Everything seemed okay except the TcpServer/TcpClient created took so much CPU time when running-- 2 of them can take 100% of my cpu time. Anyone can help?
Server creation:
string comName = "COM2";
string tcpPortNum = "5555";
string ipAddr = LocalServerIpAddress.ToString();
string initString = tcpPortNum + ";" + comName.Remove(0, 3) + ";19200,0,8,1,0,0;" + ipAddr + ";0;0";
// Create
int result = vspe.vspe_createDevice("TcpServer", initString);
Client Creation:
string comName = "COM3";
string tcpPortNum = "5555";
string ipAddr = remoteServerIpAddr.ToString();
string initString = ipAddr + ";" + tcpPortNum + ";" + comName.Remove(0, 3) + ";19200,0,8,1,0,0;0;30;;0;0";
// Create
int result = vspe.vspe_createDevice("TcpClient", initString);

Categories