ASP.NET Web Api - Getting Started

Welcome to the series on getting started with ASP.NET Web Api. In this article we will create a basic C# Web Api with Windows Integrated Authentication and create our first Web Api endpoint.

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is a great platform for building RESTful applications using the .NET Framework.

In this series we will learn how to:

  • Create a basic C# Web Api with Windows Integrated Authentication
  • Setup camel-cased json properties for the response
  • Setup A Standard Response
  • Solving CORS Issues When Using Credentials

Create a new Web Api Project

To make a C# application with Visual Studio:

  • Open Visual Studio 2015. Any edition will work. I am using Community Edition.
  • Click File -> New -> Project…

    New Project

  • Find and select ASP.NET Web Application, give your application a name and select ok

    1. On the left side under Installed -> Templates, select Web
    2. Select “ASP.NET Web Application”
      • Note: Your list of templates may differ but as long as your have the ASP.NET Web Application template listed we are good to go.
    3. Give the project a name (Web Api-Demo in this case)
    4. Select a location to store the project (c:\projects in this case)
    5. Uncheck the “Application Insights” box since we are not going to be using Application Insights
    6. Click the Ok button

    New Web Application

  • On the next screen, we need to select the New ASP.NET Project Options

    1. Select Web Api for the template
    2. Click on the “Change Authentication” button
      • Select “Windows Authentication”
      • Click Ok
    3. Uncheck “Host in the cloud”
    4. Click Ok to generate the project

    Web App Options

  • You project has now been generated and you should see a screen similar to the follow in Visual Studio

    Generated Project

  • If you hit F5 your default browser will launch with the Web Api Start Page.

    Initial Start Page in Chrome

  • The Web Api project comes with a couple of Web Api Endpoints and a very useful Api documentation page that shows all of the available Api endpoints. Click on the API link in the top nav bar to view the documentation page.

    Api Doc Page in Chrome

Creating Our First Controller

Now that we have a working Web Api project, lets add some functionality to it. The first thing we are going to do is to create a controller that will return back the logged in Windows user using Windows Integrated Authentication.

Note: You should only uses Windows Integrated Authentication within a business and not for a public Api.

  1. In the Solution Explorer, right-click on the Controllers
  2. Select Add from the menu the comes up
  3. Select “Controller…” from the list of templates

Web Api New Controller

This will open up the Controller type options.

  1. Select “Web API 2 Controller - Empty”
  2. Click the Add button

    Web Api Controller Template Selection

Next you will need to input the file name.

  1. Change the file name to FirstController
  2. Click the Add button

    Web Api Controller File Name

We now have a blank Web Api Controller that is ready for us to create methods within.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Web Api_Demo.Controllers
{
    public class FirstController : ApiController
    {
    }
}

We are now going to add a GET method that will return back the logged in user.

public string Get()
{
    return RequestContext.Principal.Identity.Name;
}

Before we test our new controller, we need to make sure that Windows Authentication is enabled and Anonymous Authentication is disabled.

  1. Open Solution Explorer, select the Solution and press F4 to open the Properties window
  2. Set “Anonymous Authentication” to disabled
  3. Set “Windows Integrated” to enabled

VS Solution Properties Set Dev Server Authentication Options

Now we are ready to test our Api. In Visual Studio, press F5 to start up a debugging session. This will launch your default web browser. Once the initial page for Web Api has loaded navigate to the /api/first page (e.g. http://localhost:58842/api/First). Your port number will be different than mine.

The response you get back will be an xml document that contains a string with your domain and user name that you are logged in with. In this case it is [Your Domain]/[Your User Name]. It will look similar to below.

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">[Your Domain]/[Your User Name]</string>

Conclusion

In this guide we learned how to create a basic C# Web Api project that uses Windows Integrated Authentication. In the next guide, we will learn how to convert the JSON responses to be camel cased instead of following the .NET pascal case convention without having to update all of our .NET class.

Series Post

Dialogue & Discussion

back to top 