1

After recently converting to .NET Core I get the following error when trying to authenticate with any of our AuthProviders:

"Error trying to resolve Service 'System.Boolean' from Adapter 'NetCoreContainerAdapter': Object reference not set to an instance of an object."

I am using ServiceStack.Core 5.9.2 on .NET Core 3.1.

Stacktrace

   at Funq.Container.GetEntry[TService,TFunc](String serviceName, Boolean throwIfMissing) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Funq\Container.cs:line 490
   at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Funq\Container.cs:line 183
   at Funq.Container.TryResolveNamed[TService](String name) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Funq\Container.Overloads.cs:line 389
   at Funq.Container.TryResolve[TService]() in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Funq\Container.Overloads.cs:line 336
   at Funq.Container.<>c__DisplayClass16_0`4.<ReverseLazyResolve>b__1(TArg1 a1, TArg2 a2, TArg3 a3) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Funq\Container.Adapter.cs:line 125
   at ServiceStack.Auth.AuthProvider.IsAccountLocked(IAuthRepository authRepo, IUserAuth userAuth, IAuthTokens tokens) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\AuthProvider.cs:line 359
   at Authentication.AuthUtilities.HandleSuccessfulTryAuthenticate(CredentialsAuthProvider authProvider, IServiceBase authService, User user, IDbConnection db) in AuthUtilities.cs:line 100
   at Authentication.AuthProviders.SgCredentialsAuthProvider.TryAuthenticate(IServiceBase authService, String userName, String password) in SGCredentialsAuthProvider.cs:line 31
   at ServiceStack.Auth.CredentialsAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, String userName, String password, String referrerUrl) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\CredentialsAuthProvider.cs:line 120
   at ServiceStack.Auth.CredentialsAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, Authenticate request) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\CredentialsAuthProvider.cs:line 98
   at ServiceStack.Auth.AuthenticateService.Authenticate(Authenticate request, String provider, IAuthSession session, IAuthProvider oAuthConfig) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\AuthenticateService.cs:line 413
   at ServiceStack.Auth.AuthenticateService.Post(Authenticate request) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Auth\AuthenticateService.cs:line 218
glazjoon
  • 518
  • 2
  • 6
  • 15

1 Answers1

1

Do you have your SgCredentialsAuthProvider registered in the IOC? It's only supposed to be added as an AuthProvider in your AuthFeature plugin registration.

The error suggests you have an autowired Service or a dependency with a public bool property or constructor that is trying to be injected with a non-existent bool dependency that's being called from the IsAccountLocked() method:

public virtual bool IsAccountLocked(IAuthRepository authRepo, IUserAuth userAuth, IAuthTokens tokens=null)
{
    if (AccountLockedValidator != null)
        return AccountLockedValidator(authRepo, userAuth, tokens);
    
    return userAuth?.LockedDate != null;
}

Judging by its implementation the IOC is trying to resolve each generic argument of the AccountLockedValidator from the IOC and failing on the bool generic argument:

public Func<IAuthRepository, IUserAuth, IAuthTokens, bool> AccountLockedValidator { get; set; }

Removing the IOC registration of your SgCredentialsAuthProvider or ignoring it if you're doing some auto scanning / auto wiring of dependencies should resolve it.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • 1
    In .NET Framework we used Container.Autowire(provider) as a quick and easy way to assign all public properties of our providers. But it seems something has changed that makes this no longer possible. Thanks for lightning fast help! – glazjoon Nov 24 '20 at 13:38
  • 1
    @glazjoon Yeah that's typical for IOC dependencies, but not for AuthProviders which should only be registered in the AuthFeature plugin. If you ever need to access an AuthProvider class at runtime you can use `AuthenticateService.GetAuthProviders()` API. – mythz Nov 24 '20 at 13:42