Skip to content

Commit ce9cb27

Browse files
Семёновых Алексейkevinchalet
authored andcommitted
Update the Vkontakte provider to extract the email address from the token response
1 parent 678a4d2 commit ce9cb27

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
4949
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
5050
var user = (JObject) payload["response"][0];
5151

52+
if (tokens.Response["email"] != null)
53+
{
54+
user.Add("email", tokens.Response["email"]);
55+
}
56+
5257
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, VkontakteAuthenticationHelper.GetId(user), Options.ClaimsIssuer)
58+
.AddOptionalClaim(ClaimTypes.Email, VkontakteAuthenticationHelper.GetEmail(user), Options.ClaimsIssuer)
5359
.AddOptionalClaim(ClaimTypes.GivenName, VkontakteAuthenticationHelper.GetFirstName(user), Options.ClaimsIssuer)
5460
.AddOptionalClaim(ClaimTypes.Surname, VkontakteAuthenticationHelper.GetLastName(user), Options.ClaimsIssuer)
5561
.AddOptionalClaim(ClaimTypes.Hash, VkontakteAuthenticationHelper.GetHash(user), Options.ClaimsIssuer)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ public static string GetHash([NotNull] JObject user)
4242
return user.Value<string>("hash");
4343
}
4444

45+
/// <summary>
46+
/// Gets the email address associated with the logged in user.
47+
/// </summary>
48+
public static string GetEmail([NotNull] JObject user)
49+
{
50+
if (user == null)
51+
{
52+
throw new ArgumentNullException(nameof(user));
53+
}
54+
55+
return user.Value<string>("email");
56+
}
57+
4558
/// <summary>
4659
/// Gets the first name associated with the logged in user.
4760
/// </summary>

0 commit comments

Comments
 (0)