ASP.Net MVC 5 has a bunch of new features. Below are a few exciting features we are going to discuss in this article,
- ASP.NET Identity
- Attribute Routing
- Filter Overrides
- One ASP.NET
- Bootstrap
ASP.NET Identity
In ASP.NET 2.0, a simple membership provider was introduced so that a user can store credentials in SQL Server Database, and whenever a user wants to use the Application, it should provide the credentials already registered in the DB. With changing times and technology, users have become more social these days so developers like to allow users to log in into Websites using social channels like Facebook, Twitter, etc.. which is possible using new ASP.NET Identity.
Advantages of ASP.NET Identity
- ASP.NET Identity System can be used across all ASP.NET frameworks( Webforms,mvc , webapi..).
- Social login provider can be integrated very easily into MVC 5 Application (facebook, twitter).
- Persistence control – By default, ASP.NET Identity stores all the users' information in a database, by using entity framework code, which is the first approach.
a. We have the control to change the Database Schema, fields in the tables etc.
Now time for some Action has come, lets code.
- Create a new project,
- Now press Ctrl + F5 to run the Application,
- Click Log in, as shown below:
- Setting up SSL in IIS Express
We should use a secure connection to connect to the social providers as our data (credentials) is very sensitive data, so we will use https instead of http. For this, we need to setup SSL in our project.
Brief on SSL
- Secure Sockets Layer(SSL) is a standard security technology to establish an encrypted link between Webserver and a Browser.
- SSL ensures that all the data passed between a Webserver and a Browser is in encrypted format rather than simple text.
- To set up SSL, a Webserver needs a SSL certificate.
- Once SSL is implemented, Application starts using https rather than http.
- In the Solution Explorer, click ProjectName.
- Hit F4 to see the project properties and check whether the SSL enabled is false or true and if it is not enabled, then make it true.
- After enabling the SSL to true, we will get SSL URL,
- Copy the SSL URL.
- In Solution Explorer, right click project(MVCAspIdentityApp) and click properties.
- Select Web tab and paste the URL in ProjectUrl Box.This URL will be used when we configure Facebook and Google Authentication Apps.
- Now, let's test whether the Application is SSL is enabled or not as before that, put [RequireHttps] attribute on the Home Controller.
Press Ctrl + F5
- If SSL is configured the first time on your machine, it will show some popup asking whether you would like to install the certificate in your machine or not . Read the instructions and click yes.
- Observe your Browser's URL as its no more http, but https, that means SSL certificate is successfully installed in your machine and an Application is SSL is enabled.
Create Google app for OAuth 2 and connect to the project
- Navigate to https://console.developers.google.com.
- Create a Project.
- Generate the Client ID and key,
- In the Home page URL box you can put SSL enabled URL, that we got while enabling SSL(https://localhost:44300/), which is optional.
- After creating the Client ID and Client Secret, add the Authorized redirect URL to https://localhost:44300/signin-google,
- After getting the ClientID and Secret key, go to Project -> Appstart Folder -> StartupAuth.cs File,
- We will find some commented-out code to connect to the different social providers.
- Uncomment the code, put the Client ID and ClientSecret, we generated to enable Google as sign in, in our Project.
- Press Cntrl + F5 to run the Application and click login button, as shown below:
We can see Google button, enabled in our Project.
- Click Google button, it will redirect you to Gmail to enter your credentials, if you are not already, logged in.
- Sign in with your Gmail account,
- After entering the credentials, you need to give permissions to the web App that was just created.
- After clicking Register, you will be successfully logged in,
- We can see the membership database created and data is saved into that,
- For that, Goto -> Views -> Server Explorer -> Tables ->Asp.Net Users
In the next article, we will be discussing other features introduced in MVC 5.