Password Special Charecter issue in mongoDB? - c#

Error : Password Special Character issue in mongoDB
Code:
connstr="mongodb://test:test#123#67.186.193.192:27017/testdb"
Here i changed password "test#123" to "test%4123" . Because # ASCII code is %4
Now it is working fine.
MongoClient client = new MongoClient(connstr); //vss
var server = client.GetServer();
//server.Ping();
var db = client.GetDatabase(dbname);
List<KeyValuePair<string, string>> collections = new List<KeyValuePair<string, string>>();
var tableviewset = JArray.Parse(db.ListCollectionsAsync().Result.ToListAsync<BsonDocument>().Result.ToJson());
But my users will use different passwords like $,^,*,( ... etc. So in this situation what i do ?

You can do somthing like this , you can user MongoClientSettings for password and all other parameter.
private MongoClientSettings GetSecuritySettingForConnection()
{
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, port);
MongoIdentity identity = new MongoInternalIdentity(authenticationDatabase, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);
settings.Credentials = new List<MongoCredential>()
{
new MongoCredential("SCRAM-SHA-1", identity, evidence)
};
settings.MaxConnectionIdleTime = new TimeSpan(0, 2, 0);
return settings;
}
After that You can this function over here:
var client = new MongoClient(GetSecuritySettingForConnection());
Let me know if any confusion.

Related

SendGrid API Substitutions not working in C#

I created a simple email dynamic transactional email template in SendGrid with only tag "First_Name".
When I DO NOT use a substitution everything works fine i.e. I get an email. But when I do use a substitution the email fails. Here is the code that uses substitution.
var client = new SendGridClient(apiKey);
Personalization personalization = new Personalization();
personalization.Substitutions = new Dictionary<string, string>() { {"First_Name","Don" } };
var msgs = new SendGridMessage()
{
TemplateId = "d-123",
From = new EmailAddress("info#domain.us", "Name"),
Subject = "Account is ready",
ReplyTo = new EmailAddress("info#domain.us", "Name"),
Personalizations = new List<Personalization>() { personalization },
};
msgs.AddTo(new EmailAddress("user#email.com"));
msgs.AddBcc(new EmailAddress("admin#domain.us));
msgs.SetClickTracking(true, true);
var responses = await client.SendEmailAsync(msgs);
Any help will be most appreciated.

Convert IMongoCollection to ObservableCollection

Not able to convert IMongoCollection<MatchDocument> to ObservableCollection<MatchDocument>
MongoClient client = new MongoClient();
var DB = client.GetDatabase("MTR");
var Collection = DB.GetCollection<MatchDocument>("MATCHES");
App.Profiles = new ObservableCollection<MatchDocument>(Collection);
You can use Collection.Find<MatchDocument>(query).ToListAsync():
MongoClient client = new MongoClient();
var DB = client.GetDatabase("MTR");
var Collection = DB.GetCollection<MatchDocument>("MATCHES");
Task<IList<MatchDocument>> doc = await Collection.Find<MatchDocument>(query).ToListAsync();
ObservableCollection<MatchDocument> Profiles = new ObservableCollection<MatchDocument>(doc.Result);
Refer: cannot convert from 'MongoDB.Driver.IMongoCollection<>' to 'System.Collections.Generic.IEnumerable<>'

Big Query InsertAll using C#

I'm using this code while trying to insert data using Big Query.
Everything is running without any exceptions, but my table is empty.
What is the problem with my code?
string SERVICE_ACCOUNT_EMAIL = "MyAccount";
var certificate = new X509Certificate2(#"XXX.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
{
Scopes = new[] { BigqueryService.Scope.BigqueryInsertdata, BigqueryService.Scope.Bigquery }
}.FromCertificate(certificate));
// Create the service.
var service = new BigqueryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "test"
});
Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest tabreq = new Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest();
List<Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData> tabrows = new List<Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData>();
Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData rd = new Google.Apis.Bigquery.v2.Data.TableDataInsertAllRequest.RowsData();
IDictionary<string, object> r = new Dictionary<string, object>();
r.Add("Key", "Value");
rd.Json = r;
tabrows.Add(rd);
tabreq.Rows = tabrows;
tabreq.Kind = "bigquery#tableDataInsertAllRequest";
service.Tabledata.InsertAll(tabreq, "xxx", "xxx", "xxx");
Your codes looks good, but the only one problem is that you need to send data, actually you are not sending any data, test this code and let me know (I created a table with two fields P1 and P2)
var logs = new List<TableDataInsertAllRequest.RowsData>();
var theLog = new TableDataInsertAllRequest.RowsData();
theLog.Json = new Dictionary<string, object>();
theLog.Json.Add("P1", "Hola");
theLog.Json.Add("P2", "Mundo");
logs.Add(theLog);
var service = GetBigQueryService();
var content = new TableDataInsertAllRequest();
content.Rows = logs;
content.Kind = "bigquery#tableDataInsertAllRequest";
content.IgnoreUnknownValues = true;
content.SkipInvalidRows = true;
var insertTask = service.Tabledata.InsertAll(content, "Your_Project_Id", "Your_DataSet", "Your_Table");
TableDataInsertAllResponse response = insertTask.Execute();

How do I hardcode my accesskey and secret access key in my .net source code?

var ec2Client = new AmazonEC2Client();
string amiID = "ami;
string keyPairName = "aaaa";
// List<string> groups = new List<string>() ;
var launchRequest = new RunInstancesRequest()
{
ImageId = amiID,
InstanceType = "tttt",
MinCount = 1,
MaxCount = 1,
KeyName = kkkk,
SubnetId = "bbbb",
};
How do I hard-code my access key and secret access key ?
var awsCreds = new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");
var ec2Config = new AmazonEC2Config() {
// add configurations here,
// such as ServiceURL and RegionEndpoint
}
var ec2Client = new AmazonEC2Client(awsCreds, ec2Config);
Learn more:
AmazonEC2Client Class
AmazonEC2Config Class
Anyway, hard-coding access key and secret key is not recommended. Please be really cautious, especially when you are going to share your code to a public repository.

Create folder using CMIS and Sharepoint

Currently I need to create a folder on a Sharepoint repository using it CMIS implementation so I am using the web services it offers on the path http://(server)/_vti_bin/cmis/soap. I implemented the code based on this example https://chemistry.apache.org/java/opencmis-cookbook.html but Sharepoint does not return the rootFolderId so the method getChildren returns nothing. Finally based on a query using the discovery service I retrieve the root folder id, but now the problem is I can't create a sub folder. This is my code:
Object.cmisPropertyId p1 = new Object.cmisPropertyId();
p1.propertyDefinitionId = "cmis:baseTypeId";
p1.value = new string[1];
p1.value[0] = "cmis:folder";
propertiesType.Items.SetValue(p1, 0);
Object.cmisPropertyString p2 = new Object.cmisPropertyString();
p2.propertyDefinitionId = "cmis:name";
p2.value = new string[1];
p2.value[0] = "mytest";
propertiesType.Items.SetValue(p2, 1);
Object.cmisPropertyString p3 = new Object.cmisPropertyString();
p3.propertyDefinitionId = "cmis:path";
p3.value = new string[1];
p3.value[0] = "Rep/f1/mytest";
propertiesType.Items.SetValue(p3, 2);
Object.cmisPropertyId p4 = new Object.cmisPropertyId();
p4.propertyDefinitionId = "cmis:objectTypeId";
p4.value = new string[1];
p4.value[0] = "cmis:folder";
propertiesType.Items.SetValue(p4, 3);
Object.cmisPropertyId p5 = new Object.cmisPropertyId();
p5.propertyDefinitionId = "cmis:parentId";
p5.value = new string[1];
p5.value[0] = "268";
propertiesType.Items.SetValue(p5, 4);
Object.cmisPropertyString p6 = new Object.cmisPropertyString();
p6.propertyDefinitionId = "Author";
p6.value = new string[1];
p6.value[0] = "theAuthor";
propertiesType.Items.SetValue(p6, 5);
Object.cmisPropertyId p7 = new Object.cmisPropertyId();
p7.propertyDefinitionId = "cmis:allowedChildObjectTypeIds";
p7.value = new string[3];
p7.value[0] = "cmis:document";
p7.value[1] = "0x010100C98D402E3C78834C873469CE4F41E2C300B0A3B5E8A3E51543977DFDCE95850082";
p7.value[2] = "cmis:folder";
propertiesType.Items.SetValue(p7, 6);
var result = objectService.createFolder(repositoryInfo.repositoryId, propertiesType, null, null, null, null, ref extType);
It always return null, I'm not sure how to pass correctly the arguments to the service for make the creation works. I don't know where to find logs o something that tell me what I am doing wrong.
Thank you
PD: I need to use this aproach because is a modification to an existing code that already use the Sharepoint CMIS services.
You try to update readonly properties. See CMIS 1.0
http://docs.oasis-open.org/cmis/CMIS/v1.1/CMIS-v1.1.html
Sample on Java
Folder root = session.getRootFolder();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, "a new folder");
Folder newFolder = root.createFolder(properties);
The key is "session". You should create session to CMIS Repository(Atom or WSDL binding). Session will provide you ability to create/retrieve folders/documents

Categories