/// <summary>
        /// Initializes a new instance of the <see cref="OAuthEventsHandler"/> class.
        /// </summary>
        /// <param name="options">The <see cref="OAuthOptions"/> to use.</param>
        /// <param name="events">The <see cref="ExternalAuthEvents"/> to use.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use.</param>
        public OAuthEventsHandler(OAuthOptions options, ExternalAuthEvents events, ILoggerFactory loggerFactory)
        {
            _options = options;
            _wrapped = options.Events;
            _logger  = loggerFactory.CreateLogger <OAuthEventsHandler>();

            // Setup custom handlers
            OnRemoteFailure  = HandleRemoteFailure;
            OnTicketReceived = HandleTicketReceived;

            // Assign delegated handlers
            OnCreatingTicket = _wrapped.CreatingTicket;
            OnRedirectToAuthorizationEndpoint = events?.OnRedirectToOAuthAuthorizationEndpoint ?? _wrapped.RedirectToAuthorizationEndpoint;
        }
Пример #2
0
        /// <summary>
        /// Tries to configure Twitter authentication.
        /// </summary>
        /// <returns>
        /// The current <see cref="ApplicationAuthorizationBuilder"/>.
        /// </returns>
        public ApplicationAuthorizationBuilder TryAddTwitter()
        {
            string name = "Twitter";

            if (TryGetProvider(name, out ExternalSignInOptions? signInOptions))
            {
                _builder.AddTwitter(
                    (options) =>
                {
                    options.ConsumerKey         = signInOptions !.ClientId;
                    options.ConsumerSecret      = signInOptions.ClientSecret;
                    options.RetrieveUserDetails = true;
                    options.StateCookie.Name    = ApplicationCookie.State.Name;

                    options.Events.OnRemoteFailure =
                        (context) => OAuthEventsHandler.HandleRemoteFailure(
                            context,
                            options.SignInScheme !,
                            options.StateDataFormat,
                            LoggerFactory.CreateLogger(name),
                            (token) => token?.Properties?.Items);

                    ConfigureRemoteAuth(name, options);

                    // Enable hook for integration tests, if configured
                    ExternalAuthEvents events = AuthEvents;

                    if (events?.OnRedirectToTwitterAuthorizationEndpoint != null)
                    {
                        options.Events.OnRedirectToAuthorizationEndpoint = events.OnRedirectToTwitterAuthorizationEndpoint;
                    }
                });
            }

            return(this);
        }