Skip to content

Commit 75f4d3e

Browse files
Avoid KeyNotFoundException
Resolves #769.
1 parent 8b3e741 commit 75f4d3e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/AspNet.Security.OAuth.Vkontakte/VkontakteAuthenticationHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
4949
}
5050

5151
using var container = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
52-
using var enumerator = container.RootElement.GetProperty("response").EnumerateArray();
52+
53+
if (!container.RootElement.TryGetProperty("response", out var profileResponse))
54+
{
55+
if (container.RootElement.TryGetProperty("error", out var error) &&
56+
error.ValueKind is JsonValueKind.String)
57+
{
58+
throw new InvalidOperationException($"An error occurred while retrieving the user profile: {error.GetString()}");
59+
}
60+
61+
throw new InvalidOperationException("An error occurred while retrieving the user profile.");
62+
}
63+
64+
using var enumerator = profileResponse.EnumerateArray();
5365
var payload = enumerator.First();
5466

5567
var principal = new ClaimsPrincipal(identity);

0 commit comments

Comments
 (0)