ASP.NET Web API Authorization with AuthorizeAttribute

Restasp.net Web-Api

Rest Problem Overview


Using the new ASP.NET Web API beta. I can not seem to get the suggested method of authenticating users, to work. Where the suggested approach seems to be, to add the [Authorize] filter to the API controllers. For example:

[Authorize] 
public IEnumerable<Item> Get()
{
    return itemsService.GetItems();
}

This does not work as intended though. When requesting the resource, you get redirected to a login form. Which is not very suitable for a RESTful webapi.

How should I proceed with this? Will it work differently in future versions?, or should I fall back to implementing my own action filter?

Rest Solutions


Solution 1 - Rest

Double check that you are using the System.Web.Http.AuthorizeAttribute and not the System.Web.Mvc.AuthorizeAttribute. This bit me before. I know the WebAPI team is trying to pull everything together so that it is familiar to MVC users, but I think somethings are needlessly confusing.

Solution 2 - Rest

Set your authentication mode to None:

<authentication mode="None" />

> None Specifies no authentication. Your application expects only anonymous users or the application provides its own authentication.

http://msdn.microsoft.com/en-us/library/532aee0e.aspx

Of course then you have to provide some sort of authentication via headers or tokens or something. You could also specify Windows and use the built in auth via headers.

If this site is mixed between API and actual pages that do need the Forms setting, then you will need to write your own handling.

All the attribute does is return an HttpUnauthorizedResult instance, the redirection is done outside of the attribute, so its not the problem, its your authentication provider.

Solution 3 - Rest

Finally, I've found a solution at: ASP.NET MVC 4 WebAPI authorization

This article shows how you can fix this issue.

Solution 4 - Rest

You are being redirected to login page because forms authentication module does this automatically. To get rid of that behavior disable forms authentication as suggested by Paul. If you want to use more REST friendly approach you should consider implementing HTTP authorization support. Take a look at this blog post http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-membership-provider/

Solution 5 - Rest

ASP.NET 5 Introduced the new Microsoft.AspNet.Authorization System which can secure both MVC and Web API controllers.

For more see my related answer here.

Update:

At that time 2 years ago it was Microsoft.AspNetCore.Authorization.

As @Chris Haines pointed out. now it resides on Microsoft.AspNetCore.Authorization.

From .NET core 1.0 to 2.0 many namespaces have been moved i think. And spread functionality between .net classic and core was obscure. That's why Microsoft introduced the .net standard.

.net standard

Solution 6 - Rest

Also, look at my answer for: https://stackoverflow.com/questions/11775594/how-to-secure-an-asp-net-web-api/16642284#16642284

There is a NuGet package I have created which you can use for convenience.

Solution 7 - Rest

If you're using a Role, make sure you have it spelled correctly :

If your role is called 'Administrator' then this - for instance will not work :

    [System.Web.Http.Authorize(Roles = "Administator")]

Neither will this :

    [System.Web.Http.Authorize(Roles = "Administrators")]

Oops...

Solution 8 - Rest

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Produces("application/json")]
[Route("api/[controller]")]
public class CitiesController : Controller
{
        [HttpGet("[action]")]
        public IActionResult Get(long cityId) => Ok(Mapper.Map<City, CityDTO>(director.UnitOfWork.Cities.Get(cityId)));
}

Use

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

Filter with authentication type

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMorgan BengtssonView Question on Stackoverflow
Solution 1 - RestAlexGadView Answer on Stackoverflow
Solution 2 - RestPaul TyngView Answer on Stackoverflow
Solution 3 - RestMalkovView Answer on Stackoverflow
Solution 4 - RestadrinView Answer on Stackoverflow
Solution 5 - RestAnestis KivranoglouView Answer on Stackoverflow
Solution 6 - RestVarun ChatterjiView Answer on Stackoverflow
Solution 7 - RestSimon_WeaverView Answer on Stackoverflow
Solution 8 - RestRahul UttarkarView Answer on Stackoverflow