简体   繁体   English

此请求的授权已被拒绝 - 新的 Web API 项目

[英]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.我刚刚在 visual studio 2015 中创建了新的 Web API 项目(使用 MVC),出于测试目的,我运行了该项目但最终出现了错误。

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.运行该项目后,它会正确显示主页,但是当我导航到 /api/values 或 /api/values/5 时,它会给我下面的 xml 消息。

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

Can someone please help?有人可以帮忙吗? I am new to Web API.我是 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.所以我在寻找什么是错的。

In the ValuesController there is an attribute Authorize if you remove it, then it will work as home page.在 ValuesController 中有一个属性Authorize如果您删除它,那么它将用作主页。

The Authorize attribute just prevent an anonymous users from accessing the ValuesController . Authorize属性只是防止匿名用户访问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在此页面中 Individual-accounts-in-web-api解释了您需要的所有内容

It happens because you have an Authorize attribute on your ValuesController发生这种情况是因为您的ValuesController上有一个Authorize属性

[Authorize]
public class ValuesController : ApiController

Just remove [Authorize] and try again只需删除[Authorize]并重试

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根据您的编辑:您应该创建一个新用户并登录或使用@Marcus H 提到的[AllowAnonymous] 。阅读更多关于身份的信息

I got the answer here.我在这里得到了答案。

https://stackoverflow.com/a/29405794/8107314 https://stackoverflow.com/a/29405794/8107314

And it was very useful to fix my error my error修复我的错误非常有用我的错误

I just came across the same problem and found the solution:我刚遇到同样的问题并找到了解决方案:

You need to register the OAuth Token Generator and OAuth Token Consumer things before WebAPI is registered.在注册 WebAPI 之前,您需要注册 OAuth Token Generator 和 OAuth Token Consumer 东西。

Kind of makes sense if you think of this as a pipeline, where Authentication/Authorization should come before any request handling by the controllers.如果您将其视为管道,则有点有意义,其中身份验证/授权应该在控制器处理任何请求之前进行。

TL;DR: Change长话短说:改变

appBuilder.UseWebApi(config);

this.ConfigureOAuthTokenGenerator(appBuilder);
this.ConfigureOAuthConsumer(appBuilder);

To

this.ConfigureOAuthTokenGenerator(appBuilder);
this.ConfigureOAuthConsumer(appBuilder);

appBuilder.UseWebApi(config);

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.我刚刚在 Visual Studio 2015 中创建了新的 Web API 项目(使用 MVC),出于测试目的,我运行了该项目,但最终出现错误。

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.运行该项目后,它会正确显示主页,但是当我导航到 /api/values 或 /api/values/5 时,它给了我以下 xml 消息。

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

Can someone please help?有人可以帮忙吗? I am new to Web API.我是 Web API 的新手。 Please note that I don't want to remove the Authorize attribute.请注意,我不想删除 Authorize 属性。 I would like to access the resource after authorization only.我只想在授权后访问资源。 So I am looking for what is wrong.所以我正在寻找问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure AD 带有用于 Web API 的不记名令牌身份验证的 AD 无法正常工作,抛出错误,因为“此请求的授权已被拒绝”。 - Azure AD with Bearer token authentication for Web API not working throwing error as “Authorization has been denied for this request.” IdentityServer3 .Net Web API。 收到错误-该请求的授权被拒绝 - IdentityServer3 .Net Web API. Getting error - Authorization has been denied for this request 此请求的授权已被拒绝 - 桌面到 ASP.NET Web API - Authorization has been denied for this request - Desktop to ASP.NET Web API 此请求的C#授权被拒绝 - C# Authorization has been denied for this request 此请求的授权已被拒绝。 邮差 - Authorization has been denied for this request. Postman OAuth令牌授权(请求已被拒绝) - OAuth token authorization (request has been denied) httpclient令牌对此请求的授权已被拒绝 - httpclient token Authorization has been denied for this request 此请求的授权已被拒绝:JWT承载 - Authorization has been denied for this request : JWT Bearer 此请求已被拒绝授权。 总是 - Authorization has been denied for this request. Always 此请求被拒绝获得授权 - Getting Authorization has been denied for this request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM