Back to Aspnetcore

Twitter external sign-in setup with ASP.NET Core

aspnetcore/security/authentication/social/twitter-logins.md

latest5.8 KB
Original Source

Twitter external sign-in setup with ASP.NET Core

By Valeriy Novytskyy and Rick Anderson

This sample shows how to enable users to sign in with their Twitter account using a sample ASP.NET Core project created on the previous page.

[!NOTE] The Microsoft.AspNetCore.Authentication.Twitter package described below uses the OAuth 1.0 APIs provided by Twitter. Twitter has since added OAuth 2.0 APIs with a different set of functionality. The OpenIddict and AspNet.Security.OAuth.Twitter packages are community implementations that use the new OAuth 2.0 APIs.

Create the app in Twitter

Store the Twitter consumer API key and secret

Store sensitive settings such as the Twitter consumer API key and secret with Secret Manager. For this sample, use the following steps:

  1. Initialize the project for secret storage per the instructions at Enable secret storage.

  2. Store the sensitive settings in the local secret store with the secrets keys Authentication:Twitter:ConsumerKey and Authentication:Twitter:ConsumerSecret:

    dotnetcli
    dotnet user-secrets set "Authentication:Twitter:ConsumerAPIKey" "<consumer-api-key>"
    dotnet user-secrets set "Authentication:Twitter:ConsumerSecret" "<consumer-secret>"
    

[!INCLUDE]

These tokens can be found on the Keys and Access Tokens tab after creating a new Twitter application:

Configure Twitter Authentication

:::moniker range="< aspnetcore-6.0"

Add the Authentication service to the Startup.ConfigureServices:

[!code-csharp]

:::moniker-end

:::moniker range=">= aspnetcore-6.0"

[!code-csharp]

:::moniker-end

[!INCLUDE default settings configuration]

For more information on configuration options supported by Twitter authentication, see the xref:Microsoft.AspNetCore.Builder.TwitterOptions API reference. This can be used to request different information about the user.

Sign in with Twitter

Run the app and select Log in. An option to sign in with Twitter appears:

Selecting Twitter redirects to Twitter for authentication:

After entering your Twitter credentials, you are redirected back to the web site where you can set your email.

You are now logged in using your Twitter credentials:

[!INCLUDEForward request information when behind a proxy or load balancer section]

<!-- ### React to cancel Authorize External sign-in Twitter doesn't support AccessDeniedPath Rather in the twitter setup, you can provide an External sign-in homepage. The external sign-in homepage doesn't support localhost. Tested with https://cors3.azurewebsites.net/ and that works. -->

Troubleshooting

  • ASP.NET Core 2.x only: If Identity isn't configured by calling services.AddIdentity in ConfigureServices, attempting to authenticate will result in ArgumentException: The 'SignInScheme' option must be provided. The project template used in this sample ensures Identity is configured.
  • If the site database has not been created by applying the initial migration, you will get A database operation failed while processing the request error. Tap Apply Migrations to create the database and refresh to continue past the error.

Next steps

  • This article showed how you can authenticate with Twitter. You can follow a similar approach to authenticate with other providers listed on the previous page.

  • Once you publish your web site to Azure web app, you should reset the ConsumerSecret in the Twitter developer portal.

  • Set the Authentication:Twitter:ConsumerKey and Authentication:Twitter:ConsumerSecret as application settings in the Azure portal. The configuration system is set up to read keys from environment variables.

Additional resources

Multiple authentication providers