2016-10-22 109 views
3

我正在尝试为SPA创建API。我正在使用最新的.NET Core,MVC和EF。我想用JWT认证用户,所以我决定使用openiddict核心。我试着按照github pagethis blog post的例子进行设置。问题是我得到“指定的授权类型不受支持。”当请求令牌时。无法请求令牌,请求字段为空

这里的邮递员要求的截图: Postman request

这里是我的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从样品处理令牌请求。通过观察Exchange方法,它处理/connect/token请求request争论的内容,我发现它收到的所有字段空值: Debugger

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

回答

4

作为mentioned in the samples,您必须注册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() 

... 
+1

你这个混蛋。 :)你击败了我15秒。 –

+0

啊,完全错过了那部分......我甚至都不知道还有另外一个包需要:)就是这样!谢谢! :) –

+1

@HonzaKalfus请注意,只有当您希望能够绑定“OpenIdConnectRequest”参数时才需要此软件包。如果您更喜欢低级别的API,您仍然可以使用'HttpContext.GetOpenIdConnectRequest()'。 – Pinpoint

相关问题