public virtual Task Authenticated(DropboxAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            _logger.WriteVerbose("AuthenticateCoreAsync");

            AuthenticationProperties properties = null;

            try
            {
                string code  = null;
                string state = null;

                var query  = Request.Query;
                var values = query.GetValues("code");
                if (values != null && values.Count == 1)
                {
                    code = values[0];
                }
                values = query.GetValues("state");
                if (values != null && values.Count == 1)
                {
                    state = values[0];
                }

                properties = Options.StateDataFormat.Unprotect(state);
                if (properties == null)
                {
                    return(null);
                }

                var redirectUri = string.Format("{0}://{1}{2}{3}",
                                                Request.Scheme, Request.Host, RequestPathBase, Options.ReturnEndpointPath);

                var tokenRequestParameters = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("code", code),
                    new KeyValuePair <string, string>("grant_type", "authorization_code"),
                    new KeyValuePair <string, string>("client_id", Options.ClientId),
                    new KeyValuePair <string, string>("client_secret", Options.ClientSecret),
                    new KeyValuePair <string, string>("redirect_uri", redirectUri),
                };

                var requestContent = new FormUrlEncodedContent(tokenRequestParameters);

                var response =
                    await _httpClient.PostAsync(TokenUri, requestContent, Request.CallCancelled);

                response.EnsureSuccessStatusCode();
                var oauthTokenResponse = await response.Content.ReadAsStringAsync();

                var oauth2Token = JObject.Parse(oauthTokenResponse);
                var accessToken = oauth2Token["access_token"].Value <string>();

                if (string.IsNullOrWhiteSpace(accessToken))
                {
                    _logger.WriteWarning("Access token was not found");
                    return(new AuthenticationTicket(null, properties));
                }

                var testUri = AccountUri + "?access_token=" + Uri.EscapeDataString(accessToken) +
                              "&oauth_token=" + Options.ClientSecret + "&oauth_consumer_key=" + Options.ClientId;
                var graphResponse = await _httpClient.GetAsync(testUri, Request.CallCancelled);

                graphResponse.EnsureSuccessStatusCode();
                var accountString = await graphResponse.Content.ReadAsStringAsync();

                var accountInformation = JObject.Parse(accountString);

                var context = new DropboxAuthenticatedContext(Context, accountInformation, accessToken);
                context.Identity = new ClaimsIdentity(
                    new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, context.Uid, Schema, Options.AuthenticationType),
                    new Claim(ClaimTypes.Name, context.DisplayName, Schema, Options.AuthenticationType),
                    new Claim("urn:dropbox:uid", context.Uid, Schema, Options.AuthenticationType),
                    new Claim("urn:dropbox:name", context.DisplayName, Schema, Options.AuthenticationType)
                },
                    Options.AuthenticationType,
                    ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType);

                await Options.Provider.Authenticated(context);

                properties.RedirectUrl = Helpers.ReturnUri;
                context.Properties     = properties;

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteWarning("Authentication failed", ex);
                return(new AuthenticationTicket(null, properties));
            }
        }