[SOLVED] List of configured name servers must not be empty. (Parameter 'servers') – MongoDb + Blazor

Issue

I am new to Blazor and I’m trying to get my MongoDb Cluster setup with my Blazor Web Assembly app.

My issue is everytime I visit the /login page it returns this error:

enter image description here

I know the error is because of the following:
Login.razor

@code {
    private User user = new();
    private StuddyBuddyBlazor.Database.Db database = new();

    private async void HandleLogin()
    {
        var foundUsers = await database.GetUserByCredentials(user.Email ?? "", user.Password ?? "");
        if (foundUsers == null) return;

        Console.WriteLine("Hello!");
        await LocalStorage.SetItemAsync<string>("email", user.Email ?? "#");
        await LocalStorage.SetItemAsync<string>("password", user.Password ?? "#");
        await AuthStateProvider.GetAuthenticationStateAsync();
        NavManager.NavigateTo("/login/success");
    }
}

And the top of my Database file:
Database/Db.cs

        private const string ConnectionString = "mongodb+srv://user:[email protected]/users?retryWrites=true&w=majority";
        public readonly MongoClientSettings Settings = MongoClientSettings.FromConnectionString(ConnectionString);
        public readonly MongoClient mongoClient = new(ConnectionString);
        public readonly IMongoDatabase usersDb;
        public readonly IMongoCollection<BsonDocument> accountsCollection;
        public readonly IMongoCollection<BsonDocument> questionsCollection;
        
        public Db()
        {
            Settings.ServerApi = new ServerApi(ServerApiVersion.V1);
            usersDb = mongoClient.GetDatabase("users");
            accountsCollection = usersDb.GetCollection<BsonDocument>("accounts");
            questionsCollection = usersDb.GetCollection<BsonDocument>("questions");
        }

And here is the database.GetUserByCredentials() even though I’m pretty sure that’s not the cause of the error.

        public async Task<UserSignup?> GetUserByCredentials(string email, string password)
        {
            var results = await accountsCollection.FindAsync<UserSignup>(new BsonDocument { { "email", email }, { "password", password } });
            var resultList = results.ToList();
            if (resultList.Count <= 0) return null;
            return resultList[0];
        }

I’ve tried the same code in a Console App and it seems to work fine.
But as soon as I use it in Blazor WASM it doesn’t let me load the page and returns the error message above.

The expected behavior: Load the page and if the supplied login details are correct then modify the session data to say the user is logged in.

The actual behavior: The page doesn't load and returns: An unhandled error has occurred. __Reload__

Solution

I am new to Blazor and I’m trying to get my MongoDb Cluster setup with my Blazor Web Assembly app.

As the name suggests a Blazor Web Assembly Application runs in the browser i.e. inside the browser sandbox. It has no direct access to a "MongoDb Cluster". To access this cluster you need a server side API that provides the data you want, and then call that API from the application.

I’m assuming Login.razor is running in the Web Assembly application. In which case the database connection won’t work because of the reason stated above. Secondly there’s nothing setting the AuthenticationState, so the authorization controls won’t work.

Any authentication is normally handled by a server side Authentication Provider. You pass in the credentials provided and it passes back an authentication token which the application then stores and uses to make API calls. It looks like you are storing the username and password in local storage on the browser when you should be storing an authentication token – a serious security flaw.

I know the authentication process is pretty complicated if you don’t use the out-of-the-box authentication, but you need to do some more research to understand how to use your database to provide authentication.

You can of course run in Server mode, where your database is directly available.

The error you have shown is a DNS error – the application is trying to access a url that it can’t resolve into an IP Address..

Answered By – MrC aka Shaun Curtis

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *