简体   繁体   English

OAuth令牌授权(请求已被拒绝)

[英]OAuth token authorization (request has been denied)

I have a WebApi 2 and a MVC Web project in the same solution running on different IIS ports. 我在不同IIS端口上运行的同一解决方案中有一个WebApi 2和一个MVC Web项目。 After recieving my Oauth token using jQuery AJAX I still get a 401 Unauthorized error message when trying to call an authorized Controller method. 在使用jQuery AJAX收到我的Oauth令牌后,我在尝试调用授权的Controller方法时仍然收到401 Unauthorized错误消息。

在此输入图像描述

Startup: 启动:

public void Configuration(IAppBuilder app)
{
    HttpConfiguration httpConfig = new HttpConfiguration();
    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.UseWebApi(httpConfig);
}

CustomOAuthProvider: CustomOAuthProvider:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    var userManager = context.OwinContext.GetUserManager<UserManager>();
    User user = await userManager.FindAsync(context.UserName, context.Password);

    // checks with context.SetError() results.

    ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
    oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));

    var ticket = new AuthenticationTicket(oAuthIdentity, null);
    context.Validated(ticket);
}

Thinks I've tried from I get "Authorization has been denied for this request." 我认为我已经尝试了“此请求已被拒绝授权。” error message when using OWIN oAuth middleware (with separate Auth and Resource Server) : 使用OWIN oAuth中间件(具有单独的Auth和资源服务器)时出现错误消息

  1. Updating all the Owin packages to latest version (Web project does not use any Owin functionality so it is not installed here). 将所有Owin软件包更新到最新版本(Web项目不使用任何Owin功能,因此不会在此处安装)。
  2. Api and web are different projects but on same machine, so same machinekey. Api和web是不同的项目,但在同一台机器上,所以同样的机器密钥。
  3. OAuth Token configuration comes before the WebApi configuration in Startup.cs. OAuth令牌配置位于Startup.cs中的WebApi配置之前。
  4. Claims are made: oAuthIdentity consist out of a role and an admin claim ( http://schemas.microsoft.com/ws/2008/06/identity/claims/role : User) 声明:oAuthIdentity由角色和管理员声明组成( http://schemas.microsoft.com/ws/2008/06/identity/claims/role:User

Everything else works as expected (web api, cors, token generation,...), what am I doing wrong? 其他一切都按预期工作(web api,cors,token generation,......),我做错了什么? (There is a lot of code involved, so let me know if I need to place an other piece of code from my projects. (涉及到很多代码,所以如果我需要从我的项目中添加其他代码,请告诉我。

EDIT: 编辑:

Ajax call (Solution by jumuro): Ajax调用(jumuro解决方案):

var token = sessionStorage.getItem(tokenKey); // Same as the generated login token
$.ajax({
    type: 'POST',
     // Don't forget the 'Bearer '!
    beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token) },
    url: 'http://localhost:81/api/auth/test', // Authorized method
    contentType: 'application/json; charset=utf-8'
}).done(function (data) {
    //
});

You have to include an Authorization header with the bearer token in the ajax call. 您必须在ajax调用中包含带有承载令牌的Authorization标头。 Please see this reponse as an example. 请以此响应为例。 I hope it helps. 我希望它有所帮助。

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

相关问题 httpclient令牌对此请求的授权已被拒绝 - httpclient token Authorization has been denied for this request 此请求的授权已被拒绝:JWT承载 - Authorization has been denied for this request : JWT Bearer 此请求的C#授权被拒绝 - C# Authorization has been denied for this request 此请求的授权已被拒绝。 邮差 - Authorization has been denied for this request. Postman 此请求已被拒绝授权。 总是 - Authorization has been denied for this request. Always 此请求被拒绝获得授权 - Getting Authorization has been denied for this request 挥舞着“此请求的授权已被拒绝”消息 - Swagger “Authorization has been denied for this request” message 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.” 发送承载令牌时,API端点返回“此请求已拒绝授权。” - API end point returning “Authorization has been denied for this request.” when sending bearer token 无法在UseJwtBearerAuthentication中验证令牌。 授权被拒绝 - Cannot validate token in UseJwtBearerAuthentication. Authorization has been denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM