在点击api时,此请求的授权被拒绝

时间:2017-03-04 07:30:18

标签: asp.net-web-api auth0

在一个与Auth0相关的教程中关注了一个示例。我能够获得令牌,但是当我使用邮递员点击我的api并将下面的参数传递给我的标题然后我得到了#34;授权被拒绝了这个请求"错误。

GET /api/shipments HTTP/1.1
Host: localhost:36740
Accept: application/json
Content-Type: application/json
Authorization: eyJ0eXAiOiJKV1QiLCJhl GciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL215Zmlyc3RhdXRoLmV1LmF1dGgwLmNvbS8iLCJzdWIiOiJhdXRoMHw1OGI5ZTAwZjEyYzk4ZTQzODEzNTlhNDQiLCJhdWQiOiJONlNzR0dxNTJmd2NmTnRUUmRoR2Zab2VHcFBpS05aTCIsImV4cCI6MTQ4ODYxNjg5MSwiaWF0IjoxNDg4NTgwODkxfQ.16-GRBQiwEbqXLH76plOHtLGuGvoaDYKCawoRT7goyg
Cache-Control: no-cache
Postman-Token: d3622cb1-0ed8-e290-cc85-c5b10161d0c1

这是我的startup.cs中的代码

[assembly: OwinStartup(typeof(Startup))]
namespace MyProj.WebAPI
{
public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth0(app);
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config);
    }
}

}

以下是来自AuthZeroConfig类的代码

public partial class Startup
{
    private void ConfigureAuth0(IAppBuilder app)
    {
        var issuer = "https://" + ConfigurationManager.AppSettings["auth0:Domain"];
        var audience = "https://" + ConfigurationManager.AppSettings["auth0:ClientId"];
        var secret = TextEncodings.Base64.Encode(
            TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["auth0:ClientSecret"]));

        app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
        {
            AuthenticationMode = AuthenticationMode.Active,
            AllowedAudiences = new[] {audience},
            IssuerSecurityTokenProviders = new[]
            {
                new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
            }

        });
    }
}

我该怎么做才能解决这个问题?

0 个答案:

没有答案