3

我正在尝试为 SPA 创建 API。我正在使用最新的 .NET Core、MVC 和 EF。我想使用 JWT 对用户进行身份验证,所以我决定使用 openiddict 核心。我已经尝试根据github 页面此博客文章中的示例进行设置。问题是我得到“不支持指定的授权类型”。请求令牌时。

这是邮递员请求的屏幕截图: 邮递员请求

这是我的ConfigureServices方法:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();

    services.AddDbContext<ApplicationDbContext>(
        options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
    );

    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    services.AddOpenIddict<ApplicationDbContext>()
        .UseJsonWebTokens()
        .EnableTokenEndpoint("/connect/token")
        .AllowPasswordFlow()
        .DisableHttpsRequirement()  // TODO: remove in production
        .AddEphemeralSigningKey();  // TODO: replace with a certificate, this should be used for development only
}

Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseApplicationInsightsRequestTelemetry();

    app.UseApplicationInsightsExceptionTelemetry();

    // don't use identity as this is a wrapper for using cookies, not needed
    // app.UseIdentity();

    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        RequireHttpsMetadata = false,  // TODO: remove, dev only
        Audience = "http://localhost:42443/",  // TODO: ???
        Authority = "http://localhost:42443/"  // TODO: ???
    });

    app.UseOpenIddict();

    app.UseMvcWithDefaultRoute();
}

我正在使用AuthorizationController示例中的 来处理令牌请求。通过观察处理请求request的方法的参数内容,我发现它接收所有字段为空值: Exchange/connect/token调试器

我不知道为什么。根据thisthis blog post,邮递员的请求应该是正确的。哪里有问题?

4

1 回答 1

4

示例中所述,您必须注册 OpenIddict MVC 模型绑定器才能OpenIdConnectRequest用作操作参数。

引用OpenIddict.Mvc包并调用AddMvcBinders它应该可以工作:

services.AddOpenIddict<ApplicationDbContext>()
    // Register the ASP.NET Core MVC binder used by OpenIddict.
    // Note: if you don't call this method, you won't be able to
    // bind OpenIdConnectRequest or OpenIdConnectResponse parameters.
    .AddMvcBinders()

...
于 2016-10-22T19:58:56.800 回答