MVC 5 Advanced Features - Part One

ASP.Net MVC 5 has a bunch of new features. Below are a few exciting features we are going to discuss in this article,

  1. ASP.NET Identity
  2. Attribute Routing
  3. Filter Overrides
  4. One ASP.NET
  5. 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

  1. ASP.NET Identity System can be used across all ASP.NET frameworks( Webforms,mvc , webapi..).

  2. Social login provider can be integrated very easily into MVC 5 Application (facebook, twitter).

  3. 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.

  1. Create a new project,

    new

    new

    new

  2. Now press Ctrl + F5 to run the Application,

    Application

  3. Click Log in, as shown below:
    Login

  4. 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.

      1. In the Solution Explorer, click ProjectName.

      2. 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.

        app

  5. After enabling the SSL to true, we will get SSL URL,

    SSL

  6. Copy the SSL URL.

  7. In Solution Explorer, right click project(MVCAspIdentityApp) and click properties.

  8. Select Web tab and paste the URL in ProjectUrl Box.This URL will be used when we configure Facebook and Google Authentication Apps.

    Apps

  9. Now, let's test whether the Application is SSL is enabled or not as before that, put [RequireHttps] attribute on the Home Controller.

    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.

    certificate

Create Google app for OAuth 2 and connect to the project

  1. Navigate to https://console.developers.google.com.

  2. Create a Project.

    Project

    Project

    Project

  3. Generate the Client ID and key,

    Client Id

    Client Id

    Client Id

    Client Id

    Client Id

  4. In the Home page URL box you can put SSL enabled URL, that we got while enabling SSL(https://localhost:44300/), which is optional.

    page

    page

  5. After creating the Client ID and Client Secret, add the Authorized redirect URL to https://localhost:44300/signin-google,

    redirect

    redirect

  6. After getting the ClientID and Secret key, go to Project -> Appstart Folder -> StartupAuth.cs File,

    code

  7. We will find some commented-out code to connect to the different social providers.

  8. Uncomment the code, put the Client ID and ClientSecret, we generated to enable Google as sign in, in our Project.

    code

  9. Press Cntrl + F5 to run the Application and click login button, as shown below:

    login

    We can see Google button, enabled in our Project.

  10. Click Google button, it will redirect you to Gmail to enter your credentials, if you are not already, logged in.

    gmail

  11. Sign in with your Gmail account,

    Sign in

  12. After entering the credentials, you need to give permissions to the web App that was just created.

    permissions
    permissions

  13. After clicking Register, you will be successfully logged in,

    logged

  14. We can see the membership database created and data is saved into that,

  15. For that, Goto -> Views -> Server Explorer -> Tables ->Asp.Net Users

    Asp.Net Users

    Asp.Net Users

In the next article, we will be discussing other features introduced in MVC 5.