Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization has been denied for this request - New Web API Project

I just created new Web API project (using MVC) in visual studio 2015 and for the testing purpose, I ran that project but ended up below error.

After running the project, it brings up Home Page correctly but when I navigated to /api/values or /api/values/5, it gives me below xml message.

<Error>
    <Message>Authorization has been denied for this request.</Message>
</Error>

Can someone please help? I am new to Web API. Please note that I don't want to remove the Authorize attribute. I would like to access the resource after authorization only. So I am looking for what is wrong.

like image 519
atp9 Avatar asked Aug 15 '16 21:08

atp9


2 Answers

In the ValuesController there is an attribute Authorize if you remove it, then it will work as home page.

The Authorize attribute just prevent an anonymous users from accessing the ValuesController.

to work with this attribute, you need first to register a user, and then login to get user's token, then you can use the token to authorize your self and get access .

In this page Individual-accounts-in-web-api is explained all what do you need

like image 170
Tarek Abo ELkheir Avatar answered Nov 17 '22 01:11

Tarek Abo ELkheir


It happens because you have an Authorize attribute on your ValuesController

[Authorize]
public class ValuesController : ApiController

Just remove [Authorize] and try again

EDIT

According to your edit: You should create a new user and login or use [AllowAnonymous] as mentioned by @Marcus H. Read more about Identity

like image 34
Roman Marusyk Avatar answered Nov 17 '22 03:11

Roman Marusyk