使用Auth0向我的API发送授权承载accessToken时出错500

时间:2017-09-26 13:02:31

标签: c# asp.net-core asp.net-core-2.0 asp.net-core-webapi auth0

我想使用Auth0在线保护我的网络webAPI netcoreapp 2.0,所以我做了所有the steps

当我没有发送授权时,我有一个401,没关系。

当我没有[授权]

时,我有200

当我将授权标头与Bearer + accessToken(从Auth0网站上的API复制令牌)时,我有500;

有人有这个问题吗?

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();


    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

    }).AddJwtBearer(options =>
    {
        options.Authority = "https://******.eu.auth0.com/";
        options.Audience = "https://**************.azurewebsites.net/";
    });
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

    app.UseAuthentication();

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

    app.UseDeveloperExceptionPage();

    app.UseStaticFiles();

    app.UseMvcWithDefaultRoute();
}

1 个答案:

答案 0 :(得分:0)

在初创公司:

 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters =
                        new TokenValidationParameters
                        {
                            ValidateIssuer = true,
                            ValidateAudience = true,
                            ValidateLifetime = true,
                            ValidateIssuerSigningKey = true,
                            ValidIssuer = TokenConstants.ValidIssuer,
                            ValidAudience = TokenConstants.ValidAudience,
                            IssuerSigningKey = JwtSecurityKey.Create(TokenConstants.IssuerSigningKey),

                        };
                });

任何地方:

public static class JwtSecurityKey
    {
        public static SymmetricSecurityKey Create(string secret)
        {
            return new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secret));
        }
    }

我在这里创建自己的密钥,但是.NET知道如何根据.IssuerSigningKey来解密它,这只是随机字符串,可以是任何类似“IAMsuperS3cretEcnryptionKey”的东西我想通过试图破译你的.NET来解决令牌,因为它不知道它是什么,并引发内部服务器错误,你需要包括AUTH0用于您的api解密它的签名密钥。

相关问题