Search code examples
asp.net-coreauthenticationtwitter

How To Configure Twitter External Authentication in ASP.NET Core 3.0 Identity


I've added the Twitter external sign-in NuGet package to my ASP.NET Core 3 app as outlined on the following Microsoft page.

Twitter external sign-in setup with ASP.NET Core

This is a screenshot of a portion of the doc: Configure Twitter Authentication DefaultScheme

Now I'm trying to access and modify the /signin-twitter default callback that was set up automatically by the NuGet installation. Also, I want to setup my own DefaultScheme to process authentications.

I have 2 questions.

  1. How do I access and modify the code in the /signin-twitter default callback that is set up automatically by the Twitter authentication NuGet package? There is no /signin-twitter file or component in the application file structure. And a search does not find "signin-twitter" text mentioned in any file. Where is the /signin-twitter code located?

  2. In the attached screenshot, you'll see the verbiage that says "The AddAuthentication(String) overload sets the DefaultScheme property". But the doc has no example that shows how to do this. How do I set up my own DefaultScheme to process authentications using AddAuthentication(String)? And what type of code file should it be?

Any assistance will be greatly appreciated. Thank you in advance.


Solution

    1. Callback URL can be customized by adding callback options when setting up twitter authentication:

      services.AddAuthentication().AddTwitter(twitterOptions =>
      {
          ...
          twitterOptions.CallbackPath = new PathString("/signin-twitter2");
      });
      

      Default callback path "/signin-twitter" is defined from TwitterOptions.cs, which can be overridden above. The RemoteAuthenticationHandler.cs will check if a request matches this callback path and take action.

      To implement additional business logic upon successful Twitter login, define OnCreatingTicket event:

      services.AddAuthentication().AddTwitter(twitterOptions =>
      {
          ...
          twitterOptions.Events = new TwitterEvents()
          {
              OnCreatingTicket = context =>
              {
                  System.Diagnostics.Debug.WriteLine($"TwitterEvents.OnCreatingTicket: UserId = {context.UserId}");
                  System.Diagnostics.Debug.WriteLine($"TwitterEvents.OnCreatingTicket: AccessToken = {context.AccessToken}");
                  System.Diagnostics.Debug.WriteLine($"TwitterEvents.OnCreatingTicket: AccessTokenSecret = {context.AccessTokenSecret}");
                  System.Diagnostics.Debug.WriteLine($"TwitterEvents.OnCreatingTicket: User = {context.User}");
                  return Task.CompletedTask;
              }
          };
          ...
          // Commented because probably you are not asking for this
          //twitterOptions.CallbackPath = new PathString("/signin-twitter2");
      });
      
    2. Authentication Scheme can be customized like this:

      services.AddAuthentication(options => {
          ...
          options.DefaultChallengeScheme = "abc";
      })
      .AddTwitter("abc", options => {
          ...
      })
      

      You may refer to the official documentation for further illustration.