public ActionResult SubmitFacebookRegistration()
        {
            var model = FacebookAuthenticationHelper.FacebookHandshake(RedirectUrl, Request);
            SocialMediaConnectStatus status = FacebookAuthenticationHelper.Register(model, "Default");

            if (status == SocialMediaConnectStatus.Registered || status == SocialMediaConnectStatus.LoggedIn)
            {
                return(Redirect("/"));
            }

            else
            {
                //TODO: Replace with comprehensive error view;
                return(Content("Operation Failed"));
            }
        }
        internal static async Task OnAuthenticated(OAuthAuthenticatedContext context)
        {
            if (context.Principal != null)
            {
                Helpers.ThrowIfConditionFailed(() => context.AccessToken == "ValidAccessToken", "");
                Helpers.ThrowIfConditionFailed(() => FacebookAuthenticationHelper.GetEmail(context.User) == "*****@*****.**", "");
                Helpers.ThrowIfConditionFailed(() => FacebookAuthenticationHelper.GetId(context.User) == "Id", "");
                Helpers.ThrowIfConditionFailed(() => FacebookAuthenticationHelper.GetLink(context.User) == "https://www.facebook.com/myLink", "");
                Helpers.ThrowIfConditionFailed(() => FacebookAuthenticationHelper.GetName(context.User) == "AspnetvnextTest AspnetvnextTest", "");
                Helpers.ThrowIfConditionFailed(() => FacebookAuthenticationHelper.GetUserName(context.User) == "AspnetvnextTest.AspnetvnextTest.7", "");
                Helpers.ThrowIfConditionFailed(() => context.User.SelectToken("id").ToString() == FacebookAuthenticationHelper.GetId(context.User), "");
                Helpers.ThrowIfConditionFailed(() => context.ExpiresIn.Value == TimeSpan.FromSeconds(100), "");
                Helpers.ThrowIfConditionFailed(() => context.AccessToken == "ValidAccessToken", "");
                context.Principal.Identities.First().AddClaim(new Claim("ManageStore", "false"));
            }

            await Task.FromResult(0);
        }
        public async Task <FacebookAudienceResponse> CreateCustomAudienceIntegration(FacebookAudienceManager audienceManager)
        {
            if (audienceManager?.Account == null)
            {
                throw new Exception("null audienceManager or audienceManager.Account");
            }
            var facebookAuthenticaionHelper = new FacebookAuthenticationHelper(_configuration);
            var token = await facebookAuthenticaionHelper.GetAccessTokenForFacebookMilestonesAsync()
                        .ConfigureAwait(false);

            HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            var stringContent = new StringContent(JsonConvert.SerializeObject(audienceManager), Encoding.UTF8, "application/json");
            var response      = await HttpClient.PostAsync("https://iapi-teamtardis.dealersocket.engineering/webapi/facebookAudienceManager", stringContent).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var stringToParse  = JsonConvert.DeserializeObject <string>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                var customAudience = JsonConvert.DeserializeObject <FacebookAudienceResponse>(stringToParse);
                return(customAudience);
            }

            throw new Exception(
                      $"Reason: {response.ReasonPhrase}, Content: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}");
        }
        protected override async Task <AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
        {
            log.LogDebug("CreateTicketAsync called");
            //Options.AuthenticationScheme = AuthenticationScheme.External;

            var endpoint = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken);

            if (Options.SendAppSecretProof)
            {
                endpoint = QueryHelpers.AddQueryString(endpoint, "appsecret_proof", GenerateAppSecretProof(tokens.AccessToken));
            }

            var response = await Backchannel.GetAsync(endpoint, Context.RequestAborted);

            response.EnsureSuccessStatusCode();

            var payload = JObject.Parse(await response.Content.ReadAsStringAsync());

            var notification = new OAuthAuthenticatedContext(Context, Options, Backchannel, tokens, payload)
            {
                Properties = properties,
                Principal  = new ClaimsPrincipal(identity)
            };

            var identifier = FacebookAuthenticationHelper.GetId(payload);

            if (!string.IsNullOrEmpty(identifier))
            {
                log.LogDebug("CreateTicketAsync FacebookAuthenticationHelper.GetId(payload) " + identifier);

                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, identifier, ClaimValueTypes.String, Options.ClaimsIssuer));
            }

            var userName = FacebookAuthenticationHelper.GetUserName(payload);

            if (!string.IsNullOrEmpty(userName))
            {
                log.LogDebug("CreateTicketAsync FacebookAuthenticationHelper.GetUserName(payload) " + userName);

                identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, userName, ClaimValueTypes.String, Options.ClaimsIssuer));
            }

            var email = FacebookAuthenticationHelper.GetEmail(payload);

            if (!string.IsNullOrEmpty(email))
            {
                log.LogDebug("CreateTicketAsync FacebookAuthenticationHelper.GetEmail(payload) " + email);

                identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer));
            }

            var name = FacebookAuthenticationHelper.GetName(payload);

            if (!string.IsNullOrEmpty(name))
            {
                log.LogDebug("CreateTicketAsync FacebookAuthenticationHelper.GetName(payload) " + name);

                identity.AddClaim(new Claim("urn:facebook:name", name, ClaimValueTypes.String, Options.ClaimsIssuer));

                // Many Facebook accounts do not set the UserName field.  Fall back to the Name field instead.
                if (string.IsNullOrEmpty(userName))
                {
                    identity.AddClaim(new Claim(identity.NameClaimType, name, ClaimValueTypes.String, Options.ClaimsIssuer));
                }
            }

            var link = FacebookAuthenticationHelper.GetLink(payload);

            if (!string.IsNullOrEmpty(link))
            {
                log.LogDebug("CreateTicketAsync FacebookAuthenticationHelper.GetLink(payload) " + link);

                identity.AddClaim(new Claim("urn:facebook:link", link, ClaimValueTypes.String, Options.ClaimsIssuer));
            }

            log.LogDebug("CreateTicketAsync notification.Options.AuthenticationScheme " + notification.Options.AuthenticationScheme);

            await Options.Notifications.Authenticated(notification);

            ISiteSettings site = siteResolver.Resolve();

            if (site != null)
            {
                Claim siteGuidClaim = new Claim("SiteGuid", site.SiteGuid.ToString());
                if (!identity.HasClaim(siteGuidClaim.Type, siteGuidClaim.Value))
                {
                    identity.AddClaim(siteGuidClaim);
                }
            }


            log.LogDebug("CreateTicketAsync notification.Principal " + notification.Principal.Identity.Name.ToString());

            //https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNet.Authentication/AuthenticationTicket.cs
            //return new AuthenticationTicket(notification.Principal, notification.Properties, notification.Options.AuthenticationScheme);
            return(new AuthenticationTicket(notification.Principal, notification.Properties, AuthenticationScheme.External));
        }