AspNetCore.Docs/aspnetcore/security/authentication/social/index.md

95 lines
5.6 KiB
Markdown
Raw Normal View History

---
2017-07-01 07:47:15 +08:00
title: Enabling authentication using Facebook, Google and other external
2016-10-29 01:35:15 +08:00
author: rick-anderson
description:
keywords: ASP.NET Core,authentication,social,authentication providers,google,facebook,twitter,microsoft account
2016-10-29 01:35:15 +08:00
ms.author: riande
manager: wpickett
2017-09-20 02:44:04 +08:00
ms.date: 11/01/2016
2016-10-29 01:35:15 +08:00
ms.topic: article
ms.assetid: eda7ee17-f38c-462e-8d1d-63f459901cf3
2016-11-17 10:52:58 +08:00
ms.technology: aspnet
ms.prod: asp.net-core
2016-12-08 04:32:53 +08:00
uid: security/authentication/social/index
2016-10-29 01:35:15 +08:00
---
2016-12-08 04:32:53 +08:00
2016-10-29 01:35:15 +08:00
# Enabling authentication using Facebook, Google and other external providers
<a name=security-authentication-social-logins></a>
By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://twitter.com/RickAndMSFT)
2016-10-29 01:35:15 +08:00
This tutorial demonstrates how to build an ASP.NET Core 2.x app that enables users to log in using OAuth 2.0 with credentials from external authentication providers.
2016-10-29 01:35:15 +08:00
[Facebook](facebook-logins.md), [Twitter](twitter-logins.md), [Google](google-logins.md), and [Microsoft](microsoft-logins.md) providers are covered in the following sections. Other providers are available in third-party packages such as [AspNet.Security.OAuth.Providers](https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers) and [AspNet.Security.OpenId.Providers](https://github.com/aspnet-contrib/AspNet.Security.OpenId.Providers).
2016-11-01 13:38:33 +08:00
![Social media icons for Facebook, Twitter, Google plus, and Windows](index/_static/social.png)
2016-11-01 13:38:33 +08:00
Enabling users to sign in with their existing credentials is convenient for the users and shifts many of the complexities of managing the sign-in process onto a third party. For examples of how social logins can drive traffic and customer conversions, see case studies by [Facebook](https://www.facebook.com/unsupportedbrowser) and [Twitter](https://dev.twitter.com/resources/case-studies).
2016-11-01 13:38:33 +08:00
Note: Packages presented here abstract a great deal of complexity of the OAuth authentication flow, but understanding the details may become necessary when troubleshooting. Many resources are available; for example, see [Introduction to OAuth 2](https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2) or [Understanding OAuth 2](http://www.bubblecode.net/2016/01/22/understanding-oauth2/). Some issues can be resolved by looking at the [ASP.NET Core source code for the provider packages](https://github.com/aspnet/Security/tree/dev/src).
2016-10-29 01:35:15 +08:00
## Create a New ASP.NET Core Project
* In Visual Studio 2017, create a new project from the Start Page, or via **File > New > Project**.
2016-10-29 01:35:15 +08:00
* Select the **ASP.NET Core Web Application** template available in **Visual C# > .NET Core** category:
2016-10-29 01:35:15 +08:00
![New Project dialog](index/_static/new-project.png)
2016-10-29 01:35:15 +08:00
* Tap **Web Application** and verify **Authentication** is set to **Individual User Accounts**:
2016-10-29 01:35:15 +08:00
![New Web Application dialog](index/_static/select-project.png)
2016-10-29 01:35:15 +08:00
Note: This tutorial applies to ASP.NET Core 2.0 SDK version which can be selected at the top of the wizard.
2016-10-29 01:35:15 +08:00
## Require SSL
2016-10-29 01:35:15 +08:00
OAuth 2.0 requires the use of SSL for authentication over the HTTPS protocol.
2016-10-29 01:35:15 +08:00
Note: Projects created using **Web Application** or **Web API** project templates for ASP.NET Core 2.x are automatically configured to enable SSL and launch with https URL if the **Individual User Accounts** option was selected on **Change Authentication dialog** in the project wizard as shown above.
2016-10-29 01:35:15 +08:00
* Learn how to enable SSL manually by following the steps in [Setting up HTTPS for development in ASP.NET Core](xref:security/https) topic.
2016-11-01 13:38:33 +08:00
* Then, require SSL on your site by following the steps in [Enforcing SSL in an ASP.NET Core app](xref:security/enforcing-ssl) topic.
2016-10-29 01:35:15 +08:00
2016-11-01 13:38:33 +08:00
## Use SecretManager to store tokens assigned by login providers
2016-10-29 01:35:15 +08:00
Social login providers assign **Application Id** and **Application Secret** tokens during the registration process (exact naming varies by provider).
2016-10-29 01:35:15 +08:00
These values are effectively the *user name* and *password* your application uses to access their API, and constitute the "secrets" that can be linked to your application configuration with the help of **Secret Manager** instead of storing them in configuration files directly or hard-coding them.
2016-10-29 01:35:15 +08:00
Follow the steps in [Safe storage of app secrets during development in ASP.NET Core](xref:security/app-secrets) topic so that you can store tokens assigned by each login provider below.
2016-10-29 01:35:15 +08:00
2016-11-01 13:38:33 +08:00
## Setup login providers required by your application
2016-10-29 01:35:15 +08:00
Use the following topics to configure your application to use the respective providers:
2016-10-29 01:35:15 +08:00
2016-11-01 13:38:33 +08:00
* [Facebook](facebook-logins.md) instructions
* [Twitter](twitter-logins.md) instructions
* [Google](google-logins.md) instructions
* [Microsoft](microsoft-logins.md) instructions
* [Other provider](other-logins.md) instructions
2016-10-29 01:35:15 +08:00
## Optionally set password
When you register with an external login provider, you do not have a password registered with the app. This alleviates you from creating and remembering a password for the site, but it also makes you dependent on the external login provider. If the external login provider is unavailable, you won't be able to log in to the web site.
2016-11-18 13:37:42 +08:00
To create a password and sign in using your email that you set during the sign in process with external providers:
* Tap the **Hello <email alias>** link at the top right corner to navigate to the **Manage** view.
![Web application Manage view](index/_static/pass1a.png)
* Tap **Create**
![Set your password page](index/_static/pass2a.png)
2016-11-18 13:37:42 +08:00
* Set a valid password and you can use this to sign in with your email.
2016-10-29 01:35:15 +08:00
## Next steps
2016-11-01 13:38:33 +08:00
* This article introduced external authentication and explained the prerequisites required to add external logins to your ASP.NET Core app.
2016-10-29 01:35:15 +08:00
* Reference provider-specific pages to configure logins for the providers required by your app.