Issue with apikey not recognized

I am a little confused about an error I am receiving.

ServiceStack.HttpError: User for ApiKey does not exist
   at ServiceStack.Auth.ApiKeyAuthProvider.Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
   at ServiceStack.Auth.AuthenticateService.Authenticate(Authenticate request, String provider, IAuthSession session, IAuthProvider oAuthConfig)
   at ServiceStack.Auth.AuthenticateService.Post(Authenticate request)
   at ServiceStack.Auth.ApiKeyAuthProvider.PreAuthenticateWithApiKey(IRequest req, IResponse res, ApiKey apiKey)
   at ServiceStack.Auth.ApiKeyAuthProvider.PreAuthenticate(IRequest req, IResponse res)
   at ServiceStack.ServiceStackHost.ApplyPreAuthenticateFilters(IRequest httpReq, IResponse httpRes)
   at AutoDialog.REDACTED.ServiceHost.ServiceStackHost.ApplyPreAuthenticateFilters(IRequest httpReq, IResponse httpRes) in D:\a\1\s\src\ServiceHost\ServiceStackHost.cs:line 58
   at ServiceStack.ServiceExtensions.GetSession(IRequest httpReq, Boolean reload)

However, when I log this call, I see following headers:

ApplyPreAuthenticateFilters Headers: [{"key":"Connection","value":"Keep-Alive"},{"key":"Authorization","value":"Bearer RWN6GAEAG6C7"},{"key":"Cookie","value":"ss-pid=sOT0MwAdZ2mPz5KmEbEw; ss-id=sK9zW6JFXUgFAkFEK4Wh; ss-opt=temp; X-UAId=6; ARRAffinity=d3331014a30dd09b8fbb05d9c792138902ef1dfab0428ed7ba951348d4eeec70"},{"key":"Host","value":"REDACTED.azurewebsites.net"},{"key":"Max-Forwards","value":"9"},{"key":"X-WAWS-Unencoded-URL","value":"/event-stream?channels=RWN6GAEAG6C7"},{"key":"X-Original-URL","value":"/event-stream?channels=RWN6GAEAG6C7"},{"key":"X-ARR-LOG-ID","value":"797b51a4-fe8c-46ca-bf5e-9cb09287917d"},{"key":"DISGUISED-HOST","value":"REDACTED.azurewebsites.net"},{"key":"X-SITE-DEPLOYMENT-ID","value":"REDACTED"},{"key":"WAS-DEFAULT-HOSTNAME","value":"REDACTED.azurewebsites.net"},{"key":"X-Forwarded-For","value":"82.143.72.154:54170,82.143.72.154"},{"key":"X-ARR-SSL","value":"2048|256|C=US, S=Washington, L=Redmond, O=Microsoft Corporation, OU=Microsoft IT, CN=Microsoft IT TLS CA 4|CN=*.azurewebsites.net"},{"key":"X-Forwarded-Proto","value":"https"},{"key":"X-MS-COLDSTART","value":"1"},{"key":"MS-ASPNETCORE-TOKEN","value":"b964e23a-66fc-4e8f-87a1-7a5395a18f94"},{"key":"X-Original-For","value":"127.0.0.1:51056"},{"key":"X-Original-Proto","value":"http"}]

So the Bearer key RWN6GAEAG6C7 is the apiKey (I did a custom implementation for generating these keys).

If I check the table ApiKey' then this key is existing with a reference to UserAuthId = 6. Checking theUserAuth` table, I see a user with id 6.

Any idea how to resolve this?

You’re receiving this error:

Indicating that the UserAuthId assigned to the API Key does not exist or it’s not being resolved from your Auth Repository.

You can test this yourself, in a different Service with:

var apiKeyRepo = (IManageApiKeys)AuthRepository;
var apiKey = apiKeyRepo.GetApiKey(request.ApiKey);
if (apiKey == null)
    throw HttpError.NotFound("API Key does not exist");
var userAuth = AuthRepository.GetUserAuth(apiKey.UserAuthId);
if (userAuth == null)
    throw HttpError.NotFound($$"User {apiKey.UserAuthId} does not exist");

Ok thanks for the hint.
Indeed the GetUserAuth did return null; this because I accidentally changed my library and renamed UserAuth to User :frowning:

1 Like