60

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?

tugberk
  • 57,477
  • 67
  • 243
  • 335
Morgan Bengtsson
  • 1,341
  • 1
  • 14
  • 23
  • 2
    Please, take a look at http://stackoverflow.com/questions/9482982/custom-mvc-authorizeattribute-for-asp-net-web-api/9484119. – paulius_l Mar 02 '12 at 10:34

7 Answers7

96

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.

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
AlexGad
  • 6,612
  • 5
  • 33
  • 45
4

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.

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
2

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/

adrin
  • 3,738
  • 8
  • 40
  • 60
1

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

Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47
  • I think you mean `Microsoft.AspNetCore.Authorization`? – Chris Haines Oct 31 '17 at 12:41
  • 1
    At that time 2 yers ago it was Microsoft.AspNetCore.Authorization.As you can see . https://www.nuget.org/packages/Microsoft.AspNet.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. I think that's why microsoft introduced the .net standard. https://learn.microsoft.com/en-us/dotnet/standard/net-standard – Anestis Kivranoglou Oct 31 '17 at 12:59
0

Also, look at my answer for: How to secure an ASP.NET Web API

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

Community
  • 1
  • 1
Varun Chatterji
  • 5,059
  • 1
  • 23
  • 24
0

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

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
0
[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

Rahul Uttarkar
  • 3,367
  • 3
  • 35
  • 40