/// <summary>
        /// For AuthenticationMode = Active, this gets invoked first when any request comes in.
        /// But its best to keep it Passive for this logic to work.
        /// This needs to go and fetch the identity claims from the 3rd party STS.
        /// </summary>
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string state = null;

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

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

                // OAuth2 10.12 CSRF
                if (!ValidateCorrelationId(properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                var thirdPartyState = properties.Dictionary[ThirdPartyStateKey];
                properties.Dictionary.Remove(ThirdPartyStateKey);
                var decodedThirdPartyState = DecodeState(thirdPartyState);

                var thirdPartyClaims = await ValidateToken(decodedThirdPartyState.ClientId, decodedThirdPartyState.Token);

                var context = new ThirdPartyAuthenticatedContext(Context)
                {
                    Identity = new ClaimsIdentity(
                        Options.AuthenticationType,
                        ClaimsIdentity.DefaultNameClaimType,
                        ClaimsIdentity.DefaultRoleClaimType)
                };
                context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, thirdPartyClaims.Claims.Single(p => p.Type == ClaimTypes.NameIdentifier).Value, XmlSchemaString, Options.AuthenticationType));

                context.Properties = properties;

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError(ex.Message);
            }

            return(new AuthenticationTicket(null, properties));
        }
 public Task Authenticated(ThirdPartyAuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }