Azure AD Auth with MSAL in Angular and ASP.NET Core API
Learn how to implement Azure AD authentication in your Angular app using MSAL and connect it securely with ASP.NET Core Web API. Step-by-step setup included.
In these days, User prefer to login through their existing credential to avoid remembering multiple username and password. Solution to this is user can log in with their Social media accounts like Facebook, Google, LinkedIn, Twitter etc.
Here is explanation on how to integrate the Facebook oauth2 in asp.net core.
Open a visual studio and create new project ”Asp.Net core web application” name it whatever you like example “OauthFacebook”
Before running the application, we need to apply migrations to our app
Go to Tools >> NuGet Package Manager >> Package Manager Console.
Enter ”Update-Database” and hit enter key . This will create identity tables.
Now run your application will open the below screen.
Note the URL from the browser address bar. In this case, the URL is https://localhost:44313/. We need this URL to configure our Facebook app which we will be doing in our next step
Go to https://developers.facebook.com/apps and login in using your Facebook account.
After logged in you can see below screen click on “Add New app” button to create new facebook App.
After clicking on Add n New App button popup window for Create App ID. Enter the App display name “OauthFacebook” with contact email and click on Create App Id.
After clicking on Create App Id show the below screen and select the facebook login application.
After that go to setting and change valid Oauth Redirect URIs
After that go to setting and change valid Oauth Redirect URIs “https://localhost:44313/signinfacebook” and click on the “Save Changes”.
After that go to setting at the top and click on the basic and see “App Id and App Secret” and copy those two value
Now we require to configure Web App to use Facebook authentication. Now right click on our project and again click on the highlight “Manage User Secret”.
{
"Authentication:Facebook:AppId": "xxxxxxxxxxxxxxxxx",
"Authentication:Facebook:AppSecret": "xxxxxxxxxxxxxxxxxxxx"
}
Now open Startup.cs file and put the following code into ConfigureServices method.
{
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
facebookOptions.AccessDeniedPath = "/AccessDeniedPathInfo";
});
}
Now the setup is done and ready to run the application.
Now run our application and click on the “Login” button at the right side on the top which redirect to below screen and click on facebook login.
When We clicked on the facebook button at that time show one popup window and clicked on the “Continue as User Name”
Click on continue as username you can able to register with Facebook account and see below screen
Now go to SQL Server Management Studio and go to “AspNetUserLogins” and execute. We can see the following detail about Facebook.
After that, execute “AspNetUsers” table and see users are login.
That is it!
Learn how to implement Azure AD authentication in your Angular app using MSAL and connect it securely with ASP.NET Core Web API. Step-by-step setup included.
Learn how to integrate Okta authentication in Angular using OAuth 2.0 and OIDC. Step-by-step guide for secure login, token handling, and route protection.angular
Implement secure JWT authentication in .NET MAUI apps. Learn best practices, tips, and examples to protect your mobile & desktop applications.
Get in touch with Prishusoft – your trusted partner for custom software development. Whether you need a powerful web application or a sleek mobile app, our expert team is here to turn your ideas into reality.