locked
MongoDB MongoAuthenticationException Catch RRS feed

  • Question

  • User718448626 posted

    How to I catch the login authentication exception for MongoDB in aspnet core 3.1 without it quiting the whole app.  The code below does trap the error but it also end the app.  

    Mongodb.Driver v2.11.0

    try
                        {
    
                            _db = client.GetDatabase(_options.Value.Database);
                            // OpenDatabase(); 
    
                            PrepareIndexes();
                            PrepareReferenceTable();
    
                            _dbStatus.Status = 4;
                            //PrepareTestDemoTable();
    
                        }
                        catch(MongoAuthenticationException authex)
                        {
                            _dbStatus.Status = 0;
                            _dbStatus.Message = authex.Message;
                            return;
                        }
                        catch(MongoException mex)
                        {
                            _dbStatus.Status = 0;
                            _dbStatus.Message = mex.Message;
                   
                        }
                        catch(Exception ex )
                        {
                             //throw new Exception("Cannot Connect to Database", ex);
                            _logger.LogDebug("Mongo dB " + ex.Message);
                            throw ex;
                        }
    MongoDB.Driver.MongoAuthenticationException
      HResult=0x80131500
      Message=Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
      Source=DataApp
      StackTrace:
       at CompanyApp.DataApp.Database.MongoDB.<PrepareIndexes>d__11.MoveNext() in D:\Project\DataApp\DataApp\Database\DataAppDB.cs:line 130
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 63
       at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__139_1(Object state) in /_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs:line 1954
       at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute() in /_/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs:line 879
       at System.Threading.ThreadPoolWorkQueue.Dispatch() in /_/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs:line 677
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() in /_/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs:line 29
    
      This exception was originally thrown at this call stack:
        MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ProcessReply(MongoDB.Driver.Core.Connections.ConnectionId, MongoDB.Driver.Core.WireProtocol.Messages.ReplyMessage<MongoDB.Bson.RawBsonDocument>)
        MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol<TCommandResult>.ExecuteAsync(MongoDB.Driver.Core.Connections.IConnection, System.Threading.CancellationToken)
        System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in ExceptionDispatchInfo.cs
        System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) in TaskAwaiter.cs
        MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(MongoDB.Driver.Core.Connections.IConnection, MongoDB.Driver.Core.Connections.ConnectionDescription, System.Threading.CancellationToken)
    
    Inner Exception 1:
    MongoCommandException: Command saslStart failed: Authentication failed..
    




    Tuesday, August 4, 2020 10:30 AM

All replies

  • User718448626 posted

    I found the problem.  Took me a while to troubleshoot.   So now it is never ever have a method with async void where possible.  The method PrepareIndexes() has signature private async void PrepareIndexes() and this resulted in ability to called the method without the await keyword. 

     It runs good but once the username and password does not match it will crash the application.

    Wednesday, August 5, 2020 12:44 AM