public async Task<ExternalUserInformation> GetExternalUserInformation(ExternalLoginInfo externalLoginInfo)
        {
            var externalUserInformation = new ExternalUserInformation();

            var userId = externalLoginInfo.Principal.FindFirstValue("urn:twitter:userid");
            var screenName = externalLoginInfo.Principal.FindFirstValue("urn:twitter:screenname");

            var twitterAccount = await twitterRepository.GetTwitterAccount(userId, screenName);

            if (twitterAccount != null && twitterAccount.User != null)
            {
                var twitterUser = twitterAccount.User;
                externalUserInformation.Email = twitterUser.Email;

                if (!string.IsNullOrEmpty(twitterUser.Name))
                {
                    var array = twitterUser.Name.Split(' ');
                    if (array.Length > 1)
                    {
                        externalUserInformation.FirstName = array[0];
                        externalUserInformation.LastName = array[1];
                    }
                }
            }

            return externalUserInformation;
        }
        public Task<ExternalUserInformation> GetExternalUserInformation(ExternalLoginInfo externalLoginInfo)
        {
            var externalUserInformation = new ExternalUserInformation
            {
                Email = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email),
                FirstName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.GivenName),
                LastName = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Surname)
            };

            return Task.FromResult(externalUserInformation);
        }
        public Task<ExternalUserInformation> GetExternalUserInformation(ExternalLoginInfo externalLoginInfo)
        {
            var externalUserInformation = new ExternalUserInformation { Email = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email) };

            var name = externalLoginInfo.Principal.FindFirstValue(ClaimTypes.Name);
            if (string.IsNullOrEmpty(name))
            {
                return Task.FromResult(externalUserInformation);
            }

            var array = name.Split(' ');
            if (array.Length < 2)
            {
                return Task.FromResult(externalUserInformation);
            }

            externalUserInformation.FirstName = array[0];
            externalUserInformation.LastName = array[1];

            return Task.FromResult(externalUserInformation);
        }
    private static void RetrieveFirstAndLastNameFromExternalPrincipal(ExternalLoginInfo externalLoginInfo, out string firstName, out string lastName)
    {
      var name = externalLoginInfo.Principal.FindFirstValue(System.Security.Claims.ClaimTypes.Name);

      firstName = string.Empty;
      lastName = string.Empty;
      if (string.IsNullOrEmpty(name))
        return;

      var array = name.Split(' ');
      firstName = array[0];
      lastName = array[1];
    }
示例#5
0
        /// <summary>
        /// Stores any authentication tokens found in the external authentication cookie into the associated user.
        /// </summary>
        /// <param name="externalLogin">The information from the external login provider.</param>
        /// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the operation.</returns>
        public virtual async Task <IdentityResult> UpdateExternalAuthenticationTokensAsync(ExternalLoginInfo externalLogin)
        {
            if (externalLogin == null)
            {
                throw new ArgumentNullException(nameof(externalLogin));
            }

            if (externalLogin.AuthenticationTokens != null && externalLogin.AuthenticationTokens.Any())
            {
                var user = await UserManager.FindByLoginAsync(externalLogin.LoginProvider, externalLogin.ProviderKey);

                if (user == null)
                {
                    return(IdentityResult.Failed());
                }

                foreach (var token in externalLogin.AuthenticationTokens)
                {
                    var result = await UserManager.SetAuthenticationTokenAsync(user, externalLogin.LoginProvider, token.Name, token.Value);

                    if (!result.Succeeded)
                    {
                        return(result);
                    }
                }
            }

            return(IdentityResult.Success);
        }
示例#6
0
        private static void RetrieveFirstAndLastNameFromExternalPrincipal(ExternalLoginInfo externalLoginInfo, out string firstName, out string lastName)
        {
            firstName = string.Empty;
            lastName = string.Empty;

            if (externalLoginInfo.LoginProvider == "Google")
            {
                firstName = externalLoginInfo.Principal.FindFirstValue(System.Security.Claims.ClaimTypes.GivenName);
                lastName = externalLoginInfo.Principal.FindFirstValue(System.Security.Claims.ClaimTypes.Surname);
            }

            if (externalLoginInfo.LoginProvider == "Facebook" || externalLoginInfo.LoginProvider == "Microsoft")
            {
                var name = externalLoginInfo.Principal.FindFirstValue(System.Security.Claims.ClaimTypes.Name);
                if (string.IsNullOrEmpty(name))
                    return;

                var array = name.Split(' ');
                if (array.Length < 2)
                    return;

                firstName = array[0];
                lastName = array[1];
            }
        }