// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount
{
///
/// Contains static methods that allow to extract user's information from a
/// instance retrieved from Microsoft after a successful authentication process.
/// http://graph.microsoft.io/en-us/docs/api-reference/v1.0/resources/user
///
public static class MicrosoftAccountHelper
{
///
/// Gets the Microsoft Account user ID.
///
public static string GetId(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value("id");
}
///
/// Gets the user's name.
///
public static string GetDisplayName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value("displayName");
}
///
/// Gets the user's given name.
///
public static string GetGivenName(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value("givenName");
}
///
/// Gets the user's surname.
///
public static string GetSurname(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value("surname");
}
///
/// Gets the user's email address.
///
public static string GetEmail(JObject user)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
return user.Value("mail") ?? user.Value("userPrincipalName");
}
}
}