The main objectives of this article are:
- Implementing Twitter Authentication.
Install Twitter Authentication NuGet Package:
Let's install the required package for Twitter authentication.
Visual Studio 2022:
Install-Package Microsoft.AspNetCore.Authentication.Twitter -Version 7.0.4
Visual Studio Code:
dotnet add package Microsoft.AspNetCore.Authentication.Twitter --version 7.0.4
Register Our App With Twitter:
To enable Twitter authentication to our application we have to register our API with Twitter. So following are steps to register with Twitter.
(Step 1)
Register with 'Twitter Developers' section and then navigate to the dashboard page 'https://developer.twitter.com/en/portal/dashboard'
(Step 2) Click on 'Create Poject' on the dashboard page.
(Step 3)
Specify the project name and then click on the 'Next' button.
(Step 4) Select a use case and then click on the 'Next' button
(Step 5)
Provide some project description and then click on 'Next' button.
(Step 6)
Next, in 'App Environment' choose 'Development' and click on 'Next' button
(Step 7) Provide the 'App Name' and click on the 'Next' button.
(Step 8):
Now copy the 'API Key' & 'ApI Secret' and then next click on 'App settings' button
(Step 9)
Under the 'User authentication settings' click on the 'Set up' button.
(Step 10)
Under 'App Permission' choose 'Read' option & check the 'Required email from users'
Under 'App Permission' choose 'Read' option & check the 'Required email from users'
(Step 11)
Under 'Type Of App' chose 'Web App, Automated, Or Bot'
(Step 12) Now provide a callback URL like '{ourdomain}/signin-twitter'(Step 3)
Now we need to provide a 'Website URL', 'Privacy Policy URL', and 'Terms of service URL', here we need to pass real domains it won't allow localhost URLs. So we can create a free websites at 'Blogspot' or 'WordPress'.
Configure Twitter Settings
Let's configure Twitter 'Api Key' & 'Api Secret' in 'appsettings.Development.json'.appsettings.Development.json:
"TwitterSettings":{ "ApiKey":"xxxxxxxxxxxxxxxxxxx", "ApISecret":"xxxxxxxxxx" }
Register Twitter Authentication Service:
Let's register the Twitter authentication service in 'Program.cs'.
Program.cs:
builder.Services.AddAuthentication() .AddTwitter(twitteOptions => { twitteOptions.ConsumerKey = builder.Configuration.GetSection("TwitterSettings") .GetValue<string>("ApiKey"); twitteOptions.ConsumerSecret = builder.Configuration.GetSection("TwitterSettings") .GetValue<string>("ApISecret"); });
- Here we passing our 'Api Key' & 'Api Secret' values to our Twitter authentication service
Test Twitter Authentication Flow:
(Step 1)
On enabling the Twitter authentication service we can see 'Twitter' login button on both 'Login' & 'Registration' pages.
(Step 2)
On clicking the 'Twitter' login button, we navigate to Twitter login page.
(Step 3)
Next, it explicitly asks us to associate an email address with our application.
(Step 4)
On clicking 'Register' button we can observe user information added to 'AspNetUser' and 'AspNetUserLogi' tables.
(Step 5)
After email confirmation, if we try to login, we can see we get authenticated as below.
In the next article, we will implement Microsoft Account authentication with Asp.Net Core Identity
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on the Asp.Net Core Identity In Razor Pages. using I love to have your feedback, suggestions, and better techniques in the comment section below
Refer:
Part-2 | Asp.Net Core Identity Series[.NET 7] | Registration Email Confirmation
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-3 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Email
Part-4 | Asp.Net Core Identity Series[.NET 7] | Sending Two-Factor Authentication(2FA) Code To Phone
Part-5 | Asp.Net Core Identity Series[.NET 7] | Google Authentication
Part - 6 | Asp.Net Core Identity Series[.NET 7] | Facebook Authentication
Part - 6 | Asp.Net Core Identity Series[.NET 7] | Facebook Authentication
Comments
Post a Comment