Skip to content

Commit 1abf8d7

Browse files
.NET 6.0 Update for Social Login Tutorials (#24242)
* Updated instructions and code snippets for social authentication in .net 6 (ms, facebook, google and twitter) * Update microsoft-logins.md Moniker issue is blocking the build, but a little strange since it looks OK to me. Testing with a more explicit > 3.0 and < 6.0 range. * Update microsoft-logins.md Reverted back to original moniker that received a build warning. * Update facebook-logins.md Removed specific mention of 3.0 * Update google-logins.md Removed specific mention of 3.0 * Update microsoft-logins.md Removed specific mention of 3.0 * Update twitter-logins.md Removed specific mention of 3.0 * .NET 6 update Social Login Tutorials * Edit pass Co-authored-by: Johan Danforth <[email protected]>
1 parent b958d26 commit 1abf8d7

File tree

9 files changed

+140
-43
lines changed

9 files changed

+140
-43
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: rick-anderson
44
description: Tutorial with code examples demonstrating the integration of Facebook account user authentication into an existing ASP.NET Core app.
55
ms.author: riande
66
ms.custom: "seoapril2019, mvc, seodec18"
7-
ms.date: 03/19/2020
7+
ms.date: 12/08/2021
88
monikerRange: '>= aspnetcore-3.0'
99
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: security/authentication/facebook-logins
@@ -17,31 +17,32 @@ By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://
1717
<!-- per @rick-anderson and scott addie, don't update images. Remove images and point the customer to the FB set up page. FB needs to maintain instructions to get key and secret.
1818
-->
1919

20-
This tutorial with code examples shows how to enable your users to sign in with their Facebook account using a sample ASP.NET Core 3.0 project created on the [previous page](xref:security/authentication/social/index). We start by creating a Facebook App ID by following the [official steps](https://developers.facebook.com).
20+
This tutorial with code examples shows how to enable your users to sign in with their Facebook account using a sample ASP.NET Core project created on the [previous page](xref:security/authentication/social/index). We start by creating a Facebook App ID by following the [official steps](https://developers.facebook.com).
2121

2222
## Create the app in Facebook
2323

2424
* Add the [Microsoft.AspNetCore.Authentication.Facebook](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Facebook) NuGet package to the project.
2525

2626
* Navigate to the [Facebook Developers app](https://developers.facebook.com/apps/) page and sign in. If you don't already have a Facebook account, use the **Sign up for Facebook** link on the login page to create one. Once you have a Facebook account, follow the instructions to register as a Facebook Developer.
2727

28-
* From the **My Apps** menu select **Create App** to create a new App ID.
29-
28+
* From the **My Apps** menu select **Create App**. The **Create an app** form appears.
3029
![Facebook for developers portal open in Microsoft Edge](index/_static/FBMyApps.png)
3130

32-
* Fill out the form and tap the **Create App ID** button.
31+
* Select an app type that best fits your project. For this project, select **Consumer**, and then **Next**. A new App ID is created.
32+
33+
* Fill out the form and tap the **Create App** button.
3334

3435
![Create a New App ID form](index/_static/FBNewAppId.png)
3536

36-
* On the new App card, select **Add a Product**. On the **Facebook Login** card, click **Set Up**
37+
* On the **Add Products to Your App** page, select **Set Up** on the **Facebook Login** card.
3738

3839
![Product Setup page](index/_static/FBProductSetup.png)
3940

4041
* The **Quickstart** wizard launches with **Choose a Platform** as the first page. Bypass the wizard for now by clicking the **FaceBook Login** **Settings** link in the menu on the lower left:
4142

4243
![Skip Quick Start](index/_static/FBSkipQuickStart.png)
4344

44-
* You are presented with the **Client OAuth Settings** page:
45+
* The **Client OAuth Settings** page is presented:
4546

4647
![Client OAuth Settings page](index/_static/FBOAuthSetup.png)
4748

@@ -50,13 +51,13 @@ This tutorial with code examples shows how to enable your users to sign in with
5051
> [!NOTE]
5152
> The URI */signin-facebook* is set as the default callback of the Facebook authentication provider. You can change the default callback URI while configuring the Facebook authentication middleware via the inherited [RemoteAuthenticationOptions.CallbackPath](/dotnet/api/microsoft.aspnetcore.authentication.remoteauthenticationoptions.callbackpath) property of the [FacebookOptions](/dotnet/api/microsoft.aspnetcore.authentication.facebook.facebookoptions) class.
5253
53-
* Click **Save Changes**.
54+
* Select **Save Changes**.
5455

55-
* Click **Settings** > **Basic** link in the left navigation.
56+
* Select **Settings** > **Basic** link in the left navigation.
5657

57-
On this page, make a note of your `App ID` and your `App Secret`. You will add both into your ASP.NET Core application in the next section:
58+
* Make a note of your `App ID` and your `App Secret`. You will add both into your ASP.NET Core application in the next section:
5859

59-
* When deploying the site you need to revisit the **Facebook Login** setup page and register a new public URI.
60+
* When deploying the site you need to revisit the **Facebook Login** setup page, and register a new public URI.
6061

6162
## Store the Facebook app ID and secret
6263

@@ -74,7 +75,9 @@ Store sensitive settings such as the Facebook app ID and secret values with [Sec
7475
7576
## Configure Facebook Authentication
7677
77-
Add the Facebook service in the `ConfigureServices` method in the *Startup.cs* file:
78+
::: moniker range="< aspnetcore-6.0"
79+
80+
Add the Authentication service to the `Startup.ConfigureServices`:
7881
7982
```csharp
8083
services.AddAuthentication().AddFacebook(facebookOptions =>
@@ -84,6 +87,16 @@ services.AddAuthentication().AddFacebook(facebookOptions =>
8487
});
8588
```
8689

90+
::: moniker-end
91+
92+
::: moniker range=">= aspnetcore-6.0"
93+
94+
Add the Authentication service to the `Program`:
95+
96+
[!code-csharp[](~/security/authentication/social/social-code/6.x/ProgramFacebook.cs)]
97+
98+
::: moniker-end
99+
87100
[!INCLUDE [default settings configuration](includes/default-settings.md)]
88101

89102
## Sign in with Facebook
@@ -106,7 +119,7 @@ The following code sets the `AccessDeniedPath` to `"/AccessDeniedPathInfo"`:
106119

107120
[!code-csharp[](~/security/authentication/social/social-code/StartupAccessDeniedPath.cs?name=snippetFB)]
108121

109-
We recommend the `AccessDeniedPath` page contain the following information:
122+
We recommend the `AccessDeniedPath` page contains the following information:
110123

111124
* Remote authentication was canceled.
112125
* This app requires authentication.
@@ -124,7 +137,7 @@ We recommend the `AccessDeniedPath` page contain the following information:
124137

125138
[!INCLUDE[](includes/chain-auth-providers.md)]
126139

127-
See the [FacebookOptions](/dotnet/api/microsoft.aspnetcore.builder.facebookoptions) API reference for more information on configuration options supported by Facebook authentication. Configuration options can be used to:
140+
For more information on configuration options supported by Facebook authentication, see the [FacebookOptions](/dotnet/api/microsoft.aspnetcore.builder.facebookoptions) API reference. Configuration options can be used to:
128141

129142
* Request different information about the user.
130143
* Add query string arguments to customize the login experience.

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,36 @@ uid: security/authentication/google-logins
1212

1313
By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://twitter.com/RickAndMSFT)
1414

15-
This tutorial shows you how to enable users to sign in with their Google account using the ASP.NET Core 3.0 project created on the [previous page](xref:security/authentication/social/index).
15+
This tutorial shows you how to enable users to sign in with their Google account using the ASP.NET Core project created on the [previous page](xref:security/authentication/social/index).
1616

1717
## Create a Google API Console project and client ID
1818

1919
* Add the [Microsoft.AspNetCore.Authentication.Google](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Google) NuGet package to the app.
20+
2021
* Follow the guidance in [Integrating Google Sign-In into your web app](https://developers.google.com/identity/sign-in/web/sign-in) (Google documentation).
21-
* In the **Credentials** page of the [Google console](https://console.developers.google.com/apis/credentials), select **CREATE CREDENTIALS** > **OAuth client ID**.
22-
* In the **Application type** dialog, select **Web application**. Provide a **Name** for the app.
22+
23+
* Go to [Google API & Services](https://console.cloud.google.com/apis).
24+
25+
* A **Project** must exist first, you may have to create one. Just a name is required. Once a project is selected, you will enter the **Dashboard**.
26+
27+
* In the **Oauth consent screen** of the **Dashboard**:
28+
* Select **User Type - External** and **CREATE**.
29+
* In the **App information** dialog, Provide an **app name** for the app, **user support email**, and **developer contact information**.
30+
* Step through the **Scopes** step.
31+
* Step through the **Test users** step.
32+
* Review the **OAuth consent screen** and go back to the app **Dashboard**.
33+
34+
35+
* In the **Credentials** tab of the application Dashboard, select **CREATE CREDENTIALS** > **OAuth client ID**.
36+
37+
* Select **Application type** > **Web application**, choose a **name**.
38+
2339
* In the **Authorized redirect URIs** section, select **ADD URI** to set the redirect URI. Example redirect URI: `https://localhost:{PORT}/signin-google`, where the `{PORT}` placeholder is the app's port.
40+
2441
* Select the **CREATE** button.
42+
2543
* Save the **Client ID** and **Client Secret** for use in the app's configuration.
44+
2645
* When deploying the site, either:
2746
* Update the app's redirect URI in the **Google Console** to the app's deployed redirect URI.
2847
* Create a new Google API registration in the **Google Console** for the production app with its production redirect URI.
@@ -45,23 +64,35 @@ You can manage your API credentials and usage in the [API Console](https://conso
4564
4665
## Configure Google authentication
4766
48-
Add the Google service to `Startup.ConfigureServices`:
67+
::: moniker range="< aspnetcore-6.0"
68+
69+
Add the Authentication service to the `Startup.ConfigureServices`:
4970
5071
[!code-csharp[](~/security/authentication/social/social-code/3.x/StartupGoogle3x.cs?highlight=11-19)]
5172
73+
::: moniker-end
74+
75+
::: moniker range=">= aspnetcore-6.0"
76+
77+
Add the Authentication service to the `Program`:
78+
79+
[!code-csharp[](~/security/authentication/social/social-code/6.x/ProgramGoogle.cs)]
80+
81+
::: moniker-end
82+
5283
[!INCLUDE [default settings configuration](includes/default-settings2-2.md)]
5384
5485
## Sign in with Google
5586
56-
* Run the app and click **Log in**. An option to sign in with Google appears.
57-
* Click the **Google** button, which redirects to Google for authentication.
87+
* Run the app and select **Log in**. An option to sign in with Google appears.
88+
* Select the **Google** button, which redirects to Google for authentication.
5889
* After entering your Google credentials, you are redirected back to the web site.
5990
6091
[!INCLUDE[Forward request information when behind a proxy or load balancer section](includes/forwarded-headers-middleware.md)]
6192
6293
[!INCLUDE[](includes/chain-auth-providers.md)]
6394
64-
See the <xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions> API reference for more information on configuration options supported by Google authentication. This can be used to request different information about the user.
95+
For more information on configuration options supported by Google authentication, see the <xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions> API reference . This can be used to request different information about the user.
6596
6697
## Change the default callback URI
6798
@@ -70,7 +101,7 @@ The URI segment `/signin-google` is set as the default callback of the Google au
70101
## Troubleshooting
71102
72103
* If the sign-in doesn't work and you aren't getting any errors, switch to development mode to make the issue easier to debug.
73-
* If Identity isn't configured by calling `services.AddIdentity` in `ConfigureServices`, attempting to authenticate results in *ArgumentException: The 'SignInScheme' option must be provided*. The project template used in this tutorial ensures that this is done.
104+
* If Identity isn't configured by calling `services.AddIdentity` in `ConfigureServices`, attempting to authenticate results in *ArgumentException: The 'SignInScheme' option must be provided*. The project template used in this tutorial ensures Identity is configured.
74105
* If the site database has not been created by applying the initial migration, you get *A database operation failed while processing the request* error. Select **Apply Migrations** to create the database, and refresh the page to continue past the error.
75106
76107
## Next steps
440 Bytes
Loading

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: rick-anderson
44
description: This sample demonstrates the integration of Microsoft account user authentication into an existing ASP.NET Core app.
55
ms.author: riande
66
ms.custom: mvc
7-
ms.date: 03/19/2020
7+
ms.date: 12/08/2021
88
monikerRange: '>= aspnetcore-3.1'
99
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: security/authentication/microsoft-logins
@@ -15,7 +15,7 @@ By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://
1515

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

18-
This sample shows you how to enable users to sign in with their work, school, or personal Microsoft account using the ASP.NET Core 3.0 project created on the [previous page](xref:security/authentication/social/index).
18+
This sample shows you how to enable users to sign in with their work, school, or personal Microsoft account using the ASP.NET Core 6.0 project created on the [previous page](xref:security/authentication/social/index).
1919

2020
## Create the app in Microsoft Developer Portal
2121

@@ -58,19 +58,19 @@ Store sensitive settings such as the Microsoft **Application (client) ID** found
5858
5959
## Configure Microsoft Account Authentication
6060
61-
Add the Microsoft Account service to the `Startup.ConfigureServices`:
61+
Add the Authentication service to the `Program`:
6262
63-
:::code language="csharp" source="~/security/authentication/social/social-code/3.x/StartupMS3x.cs" id="snippet" highlight="10-14":::
63+
[!code-csharp[](~/security/authentication/social/social-code/6.x/ProgramMS.cs)]
6464
6565
[!INCLUDE [default settings configuration](includes/default-settings.md)]
6666
6767
For more information about configuration options supported by Microsoft Account authentication, see the [MicrosoftAccountOptions](/dotnet/api/microsoft.aspnetcore.builder.microsoftaccountoptions) API reference. This can be used to request different information about the user.
6868
6969
## Sign in with Microsoft Account
7070
71-
Run the app and click **Log in**. An option to sign in with Microsoft appears. When you click on Microsoft, you are redirected to Microsoft for authentication. After signing in with your Microsoft Account, you will be prompted to let the app access your info:
72-
73-
Tap **Yes** and you will be redirected back to the web site where you can set your email.
71+
* Run the app and select **Log in**. An option to sign in with Microsoft appears.
72+
* Select to sign in with Microsoft. You are redirected to Microsoft for authentication. After signing in with your Microsoft Account, you will be prompted to let the app access your info:
73+
* Select **Yes**. You are redirected back to the web site where you can set your email.
7474
7575
You are now logged in using your Microsoft credentials:
7676
@@ -83,7 +83,9 @@ You are now logged in using your Microsoft credentials:
8383
* If the Microsoft Account provider redirects you to a sign in error page, note the error title and description query string parameters directly following the `#` (hashtag) in the Uri.
8484
8585
Although the error message seems to indicate a problem with Microsoft authentication, the most common cause is your application Uri not matching any of the **Redirect URIs** specified for the **Web** platform.
86+
8687
* 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 that this is done.
88+
8789
* 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.
8890
8991
## Next steps
@@ -149,7 +151,7 @@ For more information about configuration options supported by Microsoft Account
149151
150152
## Sign in with Microsoft Account
151153
152-
Run the app and click **Log in**. An option to sign in with Microsoft appears. When you click on Microsoft, you are redirected to Microsoft for authentication. After signing in with your Microsoft Account, you will be prompted to let the app access your info:
154+
Run the app and select **Log in**. An option to sign in with Microsoft appears. When you select on Microsoft, you are redirected to Microsoft for authentication. After signing in with your Microsoft Account, you will be prompted to let the app access your info:
153155
154156
Tap **Yes** and you will be redirected back to the web site where you can set your email.
155157
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
var services = builder.Services;
3+
var configuration = builder.Configuration;
4+
5+
services.AddAuthentication().AddFacebook(facebookOptions =>
6+
{
7+
facebookOptions.AppId = configuration["Authentication:Facebook:AppId"];
8+
facebookOptions.AppSecret = configuration["Authentication:Facebook:AppSecret"];
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
var services = builder.Services;
3+
var configuration = builder.Configuration;
4+
5+
services.AddAuthentication().AddGoogle(googleOptions =>
6+
{
7+
googleOptions.ClientId = configuration["Authentication:Google:ClientId"];
8+
googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"];
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
var services = builder.Services;
3+
var configuration = builder.Configuration;
4+
5+
services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
6+
{
7+
microsoftOptions.ClientId = configuration["Authentication:Microsoft:ClientId"];
8+
microsoftOptions.ClientSecret = configuration["Authentication:Microsoft:ClientSecret"];
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
var services = builder.Services;
3+
var configuration = builder.Configuration;
4+
5+
services.AddAuthentication().AddTwitter(twitterOptions =>
6+
{
7+
twitterOptions.ConsumerKey = configuration["Authentication:Twitter:ConsumerAPIKey"];
8+
twitterOptions.ConsumerSecret = configuration["Authentication:Twitter:ConsumerSecret"];
9+
});

0 commit comments

Comments
 (0)