In the previous article, we discussed the implementation of external authentication by using Facebook and Gmail accounts in ASP.NET Core applications. Today, we will discuss the remaining part related to the external authentication in ASP.NET Core 2.2. Now, in this article, we will discuss the process to implement authentication using Microsoft email account and Twitter account.
Prerequisites
For implementing the authentication using Microsoft Email Account and Twitter Account, we will use the same project structure which we used in the previous article. If you have not checked the previous article yet, you can from the below link.
If you want, you can download the source code provided in the above article and then try external authentication provider-based implementation as mentioned in this article.
Implement Microsoft Email Login Authentication
Step 1
If we want to implement Microsoft Email Login authentication in our web application, then we first need to create a client-id into the Microsoft Developer Portal Web site. For that, first, we need to go to the following Microsoft developer portal website - https://apps.dev.microsoft.com/
Step 2
It will redirect us to the Microsoft Account Login Form for sign-in with the existing Microsoft account.
Step 3
After a successful login, it will redirect us to the application portal's homepage.
Step 4
Now, click on the "Add an app" button.
Step 5
In the "New Application Registration" window, provide the application name and click the "Create application" button.
Step 6
After successful creation of the application, it will redirect to the Application Registration page.
Step 7
Now, click on "Add Platform" button and select a Web option.
Step 8
Now in the Redirect URLs box, provide the redirect URL appended with /signin-microsoft (as, for an example:- https://localhost:4356/signin-microsoft).
Step 9
Now, click on the "Save" button.
Step 10
Now, click on "Generate New Password" button and store the generated new password.
Step 11
Also, store the Application Id mentioned above the generated New Password button.
Step 12
Click "Save".
Step 13
Now, open the appSettings.json file and store the Application Id and Password in that file.
- {
- "ConnectionStrings": {
- "DefaultConnection": "Server=.;Database=DemoAuthentication;Trusted_Connection=True;MultipleActiveResultSets=true;user id=sa;password=xxx;"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*",
- "Authentication:Google:ClientId": "xxxxxxxxxxxxxxx.apps.googleusercontent.com",
- "Authentication:Google:ClientSecret": "xxxxxxxxxxxxxx",
- "Authentication:Facebook:AppId": "111111111111111111",
- "Authentication:Facebook:AppSecret": "2222222222222",
- "Authentication:Microsoft:AppId": "sswswsswsssssss",
- "Authentication:Microsoft:AppSecret": "yyyyyyyyyyyyyyyyyyyy"
- }
Step 14
Now, open the NuGet Package Manager Console and install the package called Microsoft.AspNetCore.Authentication.MicrosoftAccount.
Step 15
In the Startup.cs file, add the Microsoft authentication in the ConfigureService() method.
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<CookiePolicyOptions>(options =>
- {
-
- options.CheckConsentNeeded = context => true;
- options.MinimumSameSitePolicy = SameSiteMode.None;
- });
-
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(
- Configuration.GetConnectionString("DefaultConnection")));
- services.AddDefaultIdentity<IdentityUser>()
- .AddDefaultUI(UIFramework.Bootstrap4)
- .AddEntityFrameworkStores<ApplicationDbContext>();
-
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
-
- services.AddAuthentication().AddGoogle(googleOptions =>
- {
- googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
- googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- });
-
- services.AddAuthentication().AddFacebook(facebookOptions =>
- {
- facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
- facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
- });
-
- services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
- {
- microsoftOptions.ClientId = Configuration["Authentication:Microsoft:AppId"];
- microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:AppSecret"];
- });
- }
Step 16
Now, run the application and then click on the "Login" button.
Step 17
Now, click on "Microsoft" button.
Step 18
It will complete your registration with Microsoft Email Account based authentication.
Implement Twitter Login Authentication
Step 1
If we want to implement Twitter Login authentication in our web application, then we first need to create a client-id into the Twitter Application Management Web site. For that, first, we need to go to the following Twitter Application Management website - https://apps.twitter.com/
Step 2
If you don’t have any Twitter account, then first sign-up for the account. Or if have an existing account, then just simply sign-in that account. After that, it will redirect to the Twitter Application Management Page.
Step 3
Now, click on "Create an app" button.
Step 4
On the App Details page, provide the following details.
- Application Name
- Application Description
- Website URL
Step 5
Click on "Enable Sign In with Twitter" checkbox.
Step 6
Now, in the Callback URLs box, provide the redirect URL appended with /signin-twitter (as, for an example:- https://localhost:4356/signin-twitter).
Step 7
After filling up the form, click on the "Create" button.
Step 8
Now, our app has been created.
Step 9
Now, click on the "Key and tokens" tab and store the Consume API Key and API Secret Key.
Step 10
Now, return back to the Microsoft Visual Studio and open the appSettings.json file.
- {
- "ConnectionStrings": {
- "DefaultConnection": "Server=.;Database=DemoAuthentication;Trusted_Connection=True;MultipleActiveResultSets=true;user id=sa;password=xxx;"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*",
- "Authentication:Google:ClientId": "xxxxxxxxxxxxxxx.apps.googleusercontent.com",
- "Authentication:Google:ClientSecret": "xxxxxxxxxxxxxx",
- "Authentication:Facebook:AppId": "111111111111111111",
- "Authentication:Facebook:AppSecret": "2222222222222",
- "Authentication:Microsoft:AppId": "sswswsswsssssss",
- "Authentication:Microsoft:AppSecret": "yyyyyyyyyyyyyyyyyyyy",
- "Authentication:Twitter:AppId": "zzzzzzzzzzzzzzzzzzzzzzz",
- "Authentication:Twitter:AppSecret": "xxxxxxxxxxxxxxxxxx"
- }
Step 11
In this file, store the Twitter Consumer API Key and API Secret Key as a key.
Step 12
Open the NuGet Package Manager and install the Microsoft.AspNetCore.Authentication.Twitter package from there.
Step 13
In the Startup.cs file, add the Twitter authentication in the ConfigureService() method.
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<CookiePolicyOptions>(options =>
- {
-
- options.CheckConsentNeeded = context => true;
- options.MinimumSameSitePolicy = SameSiteMode.None;
- });
-
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(
- Configuration.GetConnectionString("DefaultConnection")));
- services.AddDefaultIdentity<IdentityUser>()
- .AddDefaultUI(UIFramework.Bootstrap4)
- .AddEntityFrameworkStores<ApplicationDbContext>();
-
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
-
- services.AddAuthentication().AddGoogle(googleOptions =>
- {
- googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
- googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- });
-
- services.AddAuthentication().AddFacebook(facebookOptions =>
- {
- facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
- facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
- });
-
- services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
- {
- microsoftOptions.ClientId = Configuration["Authentication:Microsoft:AppId"];
- microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:AppSecret"];
- });
-
- services.AddAuthentication().AddTwitter(twitterOptions =>
- {
- twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:AppId"];
- twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:AppSecret"];
- });
- }
Step 14
Now, run the application and then click on the "Login" button.
Step 15
Just click on the "Twitter" button.
Step 16
It will complete your registration with Twitter Account-based authentication.
Conclusion
In this article, we discussed the implementation steps related to external authentication using Microsoft and Twitter accounts. I hope this will help the readers to understand how to implement Microsoft or Twitter authentication in any application. If anybody has any queries or doubts related to this article, please let me know. Feedback is most welcome related to this article.