The documentation for the MongoDB Driver seems to differ from the actual driver pulled from NuGet.
More specifically, the documented "MongoCredentials" (plural) doesn’t exist, but only "MongoCredential" (singular). Further, MongoServer.GetDatabase doesn't seem to have a constructor that accepts MongoCredential, only MongoDatabaseSettings (alongside a string that names the databae), and I see no apparent way of giving a MongoDatabaseSettings object a MongoCredential object.
I haven't found any examples on Google with the objects I'm finding in the driver, only ones that align with the (outdated?) official documentation.
The driver that I'm using is called (in the NuGet Package Manager) "Official MongoDB C# driver."
To summarize: How does one actually provide credentials in the C# driver?
I'm also using the Official MongoDB C# driver from NuGet, version 1.8.3.
Indeed, the CSharp Driver Tutorial seems outdated.
However, the API documentation is correct; there's an entry there for the MongoCredential class (singular).
You can create a credential using either the constructor or one of the static factory methods (CreateGssapiCredential or CreateMongoCRCredential).
Next, in order to use the credentials you cannot specify them in the GetDatabase() call, but earlier, when you create the Server, like so:
var db1Credential = MongoCredential.CreateMongoCRCredential("db1", "uid", "pwd");
var db2Credential = MongoCredential.CreateMongoCRCredential("db2", "uid", "pwd");
var server = new MongoServer(
new MongoServerSettings
{
Server = new MongoServerAddress("localhost", 27017),
Credentials = new[]
{
db1Credential,
db2Credential
}
});
Related
I want to connect with Azure Cosmos DB API for MongoDB via ASP.NET app. To start I use connection string provided by Microsoft (when creating an instance by Azure)
var client = new MongoClient("mongodb://[myInstanceName]:
[primaryAccountKey]#[myInstanceName].documents.azure.com:10255/?ssl=true&retrywrites=false&replicaSet=globaldb
&maxIdleTimeMS=120000&appName=#mongocosmocoffedb#");
var database = client.GetDatabase("productdb");
var collection = database.GetCollection<Product>("productcollection");
return Ok(collection);
But when I use this connection string I got error
DirectConnection cannot be used when ConnectionModeSwitch is set to UseConnectionMode.
I found this stackoverflow topic and try use
var client = new MongoClient("mongodb://[myInstanceName]:
[primaryAccountKey]#[myInstanceName].documents.azure.com:10255/?ssl=true");
but in this case i got error
MaxWireVersion is not known.
This is the connection mode port issue. The port number you mentioned 10255 is used for geo-replication and that is causing the issue. Post numbers 10255, 10256 map to the instance that has geo-replication.
Change the port number to 10255. 10250 maps to the default Azure cosmos DB API for Mongo DB. In case of using geo-replication, public end point you can use 10255
Refer the following link: https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-sdk-connection-modes
Am new to Elastic search and NEST, I am trying to get connected with my ES server through NEST. My ES Connection initialization looks like below.
ElasticClient client = null;
public void Connect()
{
var local = new Uri("http://192.168.40.95:9200/");
var settings = new ConnectionSettings(local).DisableDirectStreaming();
client = new ElasticClient(settings);
settings.DefaultIndex("gisgcc18q4");
ReadAllData();
}
public void ReadAllData()
{
var x= client.Search<dynamic>(s=> s.MatchAll());
}
The response is attached as image below,
I am Never getting any Hits, or data. Did i made any mistake in my connector, Also please suggest me good tutorials to convert JSOn ES query to NEST as well.
Looking at the Uri in the screenshot
POST /gisgcc18q4/object/_search?typed_keys=true
suggests that you're using a version older than 7, such as 5 or 6, where document types are used. In this case, the document type name "object" has been inferred from the dynamic type passed as the generic parameter argument, but I suspect that documents have not been indexed with a document type name of "object", but something else.
If the index "gisgcc18q4" only contains one type of document, you can use
var x = client.Search<dynamic>(s=> s.MatchAll().AllTypes());
Or you can pass the specific document type name to use
var x = client.Search<dynamic>(s=> s.MatchAll().Type("_doc"));
A good getting started tutorial for the client is the elasticsearch-net-example GitHub repository. It is a walkthrough in building out a ASP.NET Core web application to search Nuget packages.
Your connection looks fine, can you please validate the detailed summary under DebugInfrormation by clicking on it and get the row query and response.
After apply same query on Postman.
Please copy and paste below expression on quick watch window on the same line which is displayed in your screenshot.
((Elasticsearch.Net.ApiCallDetails)response.ApiCall).DebugInformation
You will get the detailed information, it will be helpful for you to investigate this issue.
Recently when working with Lex in C#, I have referenced AWSCore.dll and AWSLex.dll and still trying to get a method that exposes all available Lexchatbots that I created in the Aamazon server.
var amazonPostRequest = new Amazon.Lex.Model.PostContentRequest();
var amazonPostResponse = new Amazon.Lex.Model.PostContentResponse();
used both methods to get all other information. Methods in request for bot name and alias is for setting and there is no method in response for getting available Lexchatbots in the server.
I don't believe that the Lex SDK supports this call directly.
Use the AWS Lex REST API to get a list of bots:
GET https://<your aws region endpoint>/bots/
https://docs.aws.amazon.com/lex/latest/dg/API_GetBots.html
After a long research I found the answer to my problem, It may help others.
First we need to add the AWSSDK.LexModelBuildingService through Nuget. This will add reference to the DLL.
From that all methods already exposed. We need to create both GetBotsRequest and GetBotsResponse methods.
var botRequest = new Amazon.LexModelBuildingService.Model.GetBotsRequest();
var botResponse = new Amazon.LexModelBuildingService.Model.GetBotsResponse();
Then we need to call lex model building service client
var amazonmodel = new AmazonLexModelBuildingServiceClient("YourAccesKeyId","YourSecretAccessKey",Amazon.RegionEndpoint.USEast1);
After that we can get the response of inbuilt method of GetBots()
botResponse = amazonmodel.GetBots(botRequest);
We will get the list of bots metadata
List<Amazon.LexModelBuildingService.Model.BotMetadata> bots = botResponse.Bots;
Every details about each bot created will be available in the array of list of bots
There is almost all methods in getting details from Lex configuration in LexModelBuildingService dll
Note:
In IAM (Identity Access Management) in AWS we need to give Access to have Lex components in Policy section. AWSLexFullAccess
or
atleast arn:aws:lex:region:account-id:bot:* access in policy
I'm using the Facebook C# SDK in my project and for testing purposes would like to be able to stub out the FacebookClient and insert my own fake client which will return pre-defined responses to Facebook API calls (I'm only calling the FacebookClient.Get method in my application). Achieving this is pretty easy using a factory pattern and a configurable StructureMap setup.
Apart from one thing...
My fake FacebookClient needs to return Facebook.JsonArray objects.
I've been sifting through the SDK source code, and can see that the SimpleJson class can be used to create JsonArray objects. However it is marked as internal, unless I start messing around and rebuilding the SDK.
Is there a simpler way?
You should use SimpleJson directly. You can get it via the "SimpleJson" NuGet package or on Github. Basically, we don't want people using the Facebook C# SDK as a JSON serializer - which is why we marked the methods you referenced as deprecated.
Github Source: https://github.com/facebook-csharp-sdk/simple-json
Found the answer myself; FacebookClient.DeserializeJson does the trick, although it is deprecated.
var content = /* Previously obtained JSON string */;
var client = new Facebook.FacebookClient();
var result = client.DeserializeJson(content, null);
You could also try using Facebook.Moq for testing purposes.
https://github.com/prabirshrestha/Facebook.Moq
Install-Package Facebook.Moq
var mockFb = FacebookMock.New();
mockFb
.FbSetup()
.ReturnsJson("{\"id\":\"4\",\"name\":\"Mark Zuckerberg\",\"first_name\":\"Mark\",\"last_name\":\"Zuckerberg\",\"link\":\"http:\\/\\/www.facebook.com\\/zuck\",\"username\":\"zuck\",\"gender\":\"male\",\"locale\":\"en_US\"}");
var fb = mockFb.Object;
dynamic result = fb.Get("/4");
Assert.Equal("Mark Zuckerberg", result.name);
The short question is whether is this possible and if so, how?
Outline
I have a .NET application which currently uses a service account to access information across a Google Apps domain using the Google Drive API. This works fine using the google-api-dotnet-client library and code along the same lines as shown in the samples here - which are currently a very good basic example of what I'm doing.
What I want to do now is extend it so as well as using those APIs provided by the "new" google-api-dotnet-client library, it uses the older "GData" libraries, as provided for via the
older google-gdata library, specifically the Spreadsheets API (and perhaps more to come).
The Problem
This is where the difficulty arises. The former library does exactly what I want, as evidenced by the second link in the first paragraph above - and the fact I have it doing it myself. HOWEVER... although the second library has been updated to support OAuth 2.0 in addition to OAuth 1.0 and the other older auth techniques, it does not - as far as I can tell from extensive Googling and trail-and-error - allow the "service account on behalf of all my users" operation which I need.
My question is whether I'm missing something (possibly a hard to find or undocumented something) which would allow me to do what I want. Failing that, is there any way I could force this behaviour and make these two libraries operate side by side?
The ideal solution
Ideally I would love some way of having the Google.GData.Spreadsheets.SpreadsheetsService instance be able to take advantage of the Google.Apis.Authentication.Auth2Authenticator<AssertionFlowClient> instance I'm already using... somehow. Is such witchcraft possible? I'm I missing the obvious?
Failing that, I'm happy to do the whole OAuth2 "assertion flow client" dance again if I have to, in some way that the older library can handle.
Help?
Other Thoughts
I have considered - and rejected for the time being - the option of starting from scratch and writing my own library to make this happen. This is for two reasons:
The gdata library already exists, and has been developed by many people likely cleverer than myself. I'm not so arrogant that I believe I can do better.
I'm not certain the OAuth2 with service account approach is even supported/allowed on these older APIs.
An alternate approach which I've been hoping to avoid but may have to fall back to depending on the answers here will be to use 2-legged OAuth 1.0 for portions of this. I'd prefer not to, as having parts of the app rely on one old auth method whilst other parts do it the nice new way just feels wrong to me. And there's that much more to go wrong...
Updates
I have considered the possibility of subclassing GDataRequestFactory and GDataRequest so I can make my own request factory and have that take the instance of Google.Apis.Authentication.Auth2Authenticator<AssertionFlowClient> (well, an instance of Google.Apis.Authentication.IAuthenticator anyway) which could step in to authenticate the request just before it's called. However... the constructor for GDataRequest is internal, which has stopped me.
It's really looking like this isn't meant to be.
For the sake of other folks coming across this question (now that the solution linked to in the accepted answer uses deprecated code), here's how I solved it:
First, start in "new API" land (use the Google.Apis.Auth nuget package) by setting up a ServiceAccountCredential following Google's Service Account example:
//In the old api, this accessed the main api accounts' sheets, not anymore
//** Important ** share spreadsheets with the Service Account by inviting the "serviceAccountEmail" address to the sheet
string serviceAccountEmail = "12345697-abcdefghijklmnop#developer.gserviceaccount.com";
var certificate = new X509Certificate2(#"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds" }
}.FromCertificate(certificate));
Tell the credential to request an Access Token:
credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();
Now it's time to switch back to "old API" land (use the Google.GData.Spreadsheets nuget package). Start by constructing the SpreadsheetsService (similar to Google's example):
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
To use Service Account authentication, we'll create an instance of the GDataRequestFactory and set a custom Authorization header:
var requestFactory = new GDataRequestFactory("My App User Agent");
requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken));
Finally, set the SpreadsheetsService's RequestFactory property to this new factory:
service.RequestFactory = requestFactory;
And go ahead and use the SpreadsheetsService as you would had you authenticated using any other technique. (Tip: share spreadsheets with the Service Account by inviting the serviceAccountEmail address to the sheet)
I managed to solve this by subclassing GDataRequestFactory and creating my own implementation of the interfaces implemented by GDataRequest. This implementation wraps an instance of GDataRequest instantiated via reflection, and adds in the necessary code to perform authentication using an instance of IAuthenticator (in my case, Auth2Authenticator).
I wrote a blog post on it and added an example as a Gist:
Blog: Using Google's Spreadsheet API using .NET, OAuth 2.0 and a Service Account
Gist 4244834
Feel free to use this if it helps you (BSD licence).
Hey just stumbled accross the same problem and produced a different solution:
Has anybody ever concidered of writing the parameters from the credentials-object directly to an OAuth2Parameters-Object?
I did this and it worked nicely:
public class OAuthTest
{
OAuth2Parameters param = new OAuth2Parameters();
public OAuthTest()
{
Debug.WriteLine("Calling: AuthGoogleDataInterface()");
bool init = AuthGoogleDataInterface();
if (init)
{
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "My App User Agent", this.param);
//requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken));
var service = new SpreadsheetsService("MyService");
service.RequestFactory = requestFactory;
SpreadsheetQuery query = new SpreadsheetQuery();
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.Query(query);
// Iterate through all of the spreadsheets returned
foreach (SpreadsheetEntry entry in feed.Entries)
{
// Print the title of this spreadsheet to the screen
Debug.WriteLine(entry.Title.Text);
}
}
Debug.WriteLine(m_Init);
}
private bool AuthGoogleDataInterface()
{
bool b_success;
try
{
Console.WriteLine("New User Credential");
// New User Credential
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
GoogleClientSecrets GCSecrets = GoogleClientSecrets.Load(stream);
string[] ArrScope = new[] { "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds" };
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GCSecrets.Secrets,
ArrScope,
"user", CancellationToken.None,
new FileDataStore("My.cal")).Result;
// put the Information generated for the credentials object into the OAuth2Parameters-Object to access the Spreadsheets
this.param.ClientId = GCSecrets.Secrets.ClientId; //CLIENT_ID;
this.param.ClientSecret = GCSecrets.Secrets.ClientSecret; //CLIENT_SECRET;
this.param.RedirectUri = "urn:ietf:wg:oauth:2.0:oob"; //REDIRECT_URI;
this.param.Scope = ArrScope.ToString();
this.param.AccessToken = credential.Token.AccessToken;
this.param.RefreshToken = credential.Token.RefreshToken;
}
Debug.WriteLine("AuthGoogleDataInterface: Success");
b_success = true;
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
b_success = false;
}
return b_success;
}
}