MJBZA
MJBZA

Reputation: 5018

Sequence contains no matching element while it found the element once

I am using this code for a small project to provide the jwt auth.

The problem is in case of revoking the refresh token, the following code from UsersController would fail in line 5:

1.  private (RefreshToken, Account) getRefreshToken(string token)
2.  {
3.      var account = _context.Accounts.SingleOrDefault(u => u.RefreshTokens.Any(t => t.Token == token));
4.      if (account == null) throw new AppException("Invalid token");
5.      var refreshToken = account.RefreshTokens.Single(x => x.Token == token);
6.      if (!refreshToken.IsActive) throw new AppException("Invalid token");
7.      return (refreshToken, account);
8.  }

It passes line 3 and 4, so it means it could find the RefreshToken and the related account in line 3; however, later in line 5 I have encountered with the following error:

"message": "Sequence contains no matching element"

Here is a picture of the error:

enter image description here

Upvotes: 1

Views: 747

Answers (1)

Leaky
Leaky

Reputation: 3646

If I understand correctly that RefreshTokens is a navigational property on Account entity:

Have you tried to Include() it?

_context.Accounts.Include(x => x.RefreshTokens).SingleOrDefault(u => u.RefreshTokens.Any(t => t.Token == token));

Upvotes: 1

Related Questions