In this tutorial, I will show you how to create an ASP.NET MVC 5 Application that enables a user to log in using OAuth 2.0 with some credentials from Microsoft Live. ASP.NET MVC Authentication with Microsoft Account is very helpful and useful to register an authenticated user. This is very easy to implement in ASP.NET MVC 5 application.
In this tutorial, first of all, I will create a new project with MVC template, then create an account in Microsoft, and then create a new app in Microsoft account, and then get ClientId and ClientSecret from the app, and then put it in our application’s Startup.Auth.cs file and then just simply run.
How to enable ASP.NET MVC Authentication using Microsoft Account?
This is very simple and easy, just follow the following steps, and get to know how to implement External Authentication provider (E.g. Microsoft).
Step # 1 – Create a new project.
Go to file => New => Project => ASP.NET Web Application from middle pane => Enter the name (E.g. AppWithExternalLoginProvider) in the name field => then click ok button. It will open a new dialog.
Then you will see a new dialog called “New ASP.NET Project”. Select MVC Project => and then click ok button.
Note: – Make sure that the Authentication value is set to the individual user account.
Step # 2 – Open Startup.Auth.cs File.
All of the information about user Authentication Provider for your project are stored in Startup.Auth.cs file. Just go to Solution Explorer => click on App Start folder => double click on “Startup.Auth.cs” file.
Step # 3 – Enable Microsoft Account Login.
Just go to ConfigureAuth method and locate the below line and just uncomment it with (Ctrl + K, U).
“app.UseMicrosoftAccountAuthentication”
At this time you will see there is no value of Client Id and Client Secret.
Step # 4 – Create a new Application on Microsoft Account
Go to this Link ( https://apps.dev.microsoft.com/ ) and logged In with your Microsoft account. Then it will show you a new page. Just click on “Add an App” button. It will show a new Popup. Just enter the name of your application and then click on “Create application” button.
Step # 5 – Get Client Secret and Client Id
Application Id is your Client Id and if you want to get Client Secret, then click on Generate new password button under the Application Secrets Label. It will show a new Popup.
Copy the password, it will be your Client Secret. You can generate only two Passwords in one application.
Step # 6 – Get ASP.NET MVC 5 Web application URL
Go to solution explorer => right click on project name => then choose properties (It will show a new page) => Choose the web from the left pane of the page => Copy the project URL (E.g. http://localhost:49419/ ) from the project URL field.
Step # 7 – Add Platform
In this step, we will add application platform. Just go to the “Add Platform” button under the Platforms. If you click it then it will show a new popup.
Then choose “Web” option and then enter the URL of ASP.NET MVC 5 Web application append with “signin-microsoft” (E.g. http://localhost:49419/signin-microsoft) under the Redirect URLs field.
Then go to the bottom of the page and just click on the Save button.
Step # 8 – Add Client Id and Client Secret into your project
Then just add the client Id and Client Secret into the Startup.Auth.cs file in your ASP.NET MVC 5 App project.
Step # 9 – Run your application
Now run your application. Go to the login page, and then press Microsoft button just right side of your page.
Then you will see a page that is asking ‘Let this app access your info’. Then just click on the “Yes” button.
Then it will show you a new page that will say you “You’ve successfully authenticated with Microsoft” and then will ask you “Please enter a username for this site below and click the register button to finish logging”.
Just enter your email in the email field and then click on register button.
And then finally you will see you are logged in with your Microsoft account.
Summary
Congratulation you have successfully authenticated in ASP.NET MVC 5 App using Microsoft Account. I hope this will be very helpful for you. Please like and share.
Thanks, this is very helpful.