Introduction
Authorization allows a website user to grant and restrict permissions on Web pages, functionality, and data. In this article, you will learn how to implement authorization in a Web API. Authorization checks whether a user is allowed to perform an action or has access to some functionality. For example, having permission to get data and post data is a part of authorization.
Web API uses authorization filters to implement authorization. The Authorization filters run before the controller action. If the request is not authorized, the filter returns an error response, and the action is not invoked.
Web API provides a built-in authorization filter, Authorize Attribute. This filter checks whether the user is authenticated. If not then it returns the HTTP status code 401 (Unauthorized), without invoking the action.
Getting Started
- Create a new Project. Open Visual Studio 2012.
- Go to "File" -> "New" -> "Project...".
- Select "Web" in the installed templates.
- Select "ASP.NET MVC 4 Web Application".
- Select Web API, View engine should remain Razor.
- Enter the Name and choose the location.
- Click"OK".
In this sample, I will use Knockout to display data on the client side.
First, add a model class as in the following.
Now add a class as in the following.
Now add the following controller class.
public class ValuesController : ApiController
{
private List<Employee> EmpList = new List<Employee>();
public IEnumerable<Employee> Get()
{
EmpList.Add(new Employee { EmployeeID = 1, FirstName = "Nancy", LastName = "Davolio",
City = "Seattle", Region = "WA", PostalCode = "98122", Country = "USA" });
EmpList.Add(new Employee { EmployeeID = 2, FirstName = "Andrew", LastName = "Fuller",
City = "Tacoma", Region = "WA", PostalCode = "98401", Country = "USA" });
EmpList.Add(new Employee { EmployeeID = 3, FirstName = "Janet", LastName = "Leverling",
City = "Kirkland", Region = "WA", PostalCode = "98033", Country = "USA" });
EmpList.Add(new Employee { EmployeeID = 4, FirstName = "Margaret", LastName = "Peacock",
City = "Redmond", Region = "WA", PostalCode = "98052", Country = "USA" });
EmpList.Add(new Employee { EmployeeID = 5, FirstName = "Steven", LastName = "Buchanan",
City = "London", Region = "WA", PostalCode = "SW1 8JR", Country = "UK" });
EmpList.Add(new Employee { EmployeeID = 6, FirstName = "Michael", LastName = "Suyama",
City = "London", Region = "WA", PostalCode = "EC2 7JR", Country = "UK" });
EmpList.Add(new Employee { EmployeeID = 7, FirstName = "Robert", LastName = "King",
City = "London", Region = "WA", PostalCode = "RG1 9SP", Country = "UK" });
EmpList.Add(new Employee { EmployeeID = 8, FirstName = "Laura", LastName = "Callahan",
City = "Seattle", Region = "WA", PostalCode = "98105", Country = "USA" });
EmpList.Add(new Employee { EmployeeID = 9, FirstName = "Anne", LastName = "Dodsworth",
City = "London", Region = "WA", PostalCode = "WG2 7LT", Country = "UK" });
return EmpList;
}
}
Add the following view to display data.
Now run without authorization. Let's see what we get.
![Authorization]()
Now add the Authorize attribute to the Get method.
Now run again.
![Employee details]()
If you want authorization on all the actions of a controller then put Authorize above the controller class as in the following.
You can set permission for a specific user like this.
You can provide authorization for a specific user role also.
Conclusion
In this article, we learned how to use Web API in ASP.NET authorization.