Individual Account in ASP.Net Web API

Introduction

This articles explains how to create a individual accounts in the ASP.NET Web API. This is related to the authentication and the security of the Web API. It is usually created for managing the local database. For this article we need the Fiddler tool for the HTTP request of the Web API.

Now see the following example.

Step 1

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Templates" -> "Visual C#" -> "Web" and select ASP.NET Web Application.

    Select Web Application

  • From the ASP.NET project window select "WebAPI".

    select Web API Project

  • Click on the "Change Authentication" button.
  • From the Change Authentication dialog select "Individual User Accounts".

    Change Authentication

  • Click on the "OK" button.

Step 2

Press F5 to execute the application. It displays the home page.

Execute the application

When the application is executed it uses a port number for the web server. Here the port number is "42506"

Step 3

Now send the HTTP request to the Web API.

  • Now Open the Fiddler Tool.
  • Select the "Composer" tab.
  • And type the following URL "http://localhost:42506/api/values".

    fiddler URL

  • Now click on the "Execute" button.
  • After completing the request the response is displayed in the left panel.

    Response

    It displays the 401 unauthorized because the valuesController is marked with the "[Authorize]" attribute. This attribute specifies tht the request of the controller must be from the authenticated user.

  • Now double-click on the response. The detail is displayed in the inspector tab.

    Output on fiddle

  • In the detailed View click on the "Raw" tab for displaying the HTTP response.

    Raw Http Response

Step 4

Register the user.

  • In the Fiddler tool.
  • Again click on the composer and select the "Post" method.
  • Change the URL to "http://localhost:42506/api/Account/Register".

    Register User

  • And add the following Header in the Request Header "Content-Type: application/json".
  • And write the following detail in the Request body.

    {
    "UserName": "Smith",
    "Password": "password",
    "ConfirmPassword": "password"
    }

The Fiddler Tool looks like this:

Request in Fiddler

Press "Execute" and now click on the Raw for seeing the HTTP request.

Http Request


Similar Articles