3
我正在尝试为SPA创建API。我正在使用最新的.NET Core,MVC和EF。我想用JWT认证用户,所以我决定使用openiddict核心。我试着按照github page和this blog post的例子进行设置。问题是我得到“指定的授权类型不受支持。”当请求令牌时。无法请求令牌,请求字段为空
这里是我的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
争论的内容,我发现它收到的所有字段空值:
我不知道为什么。根据this和this博客文章,邮递员请求应该是正确的。哪里有问题?
你这个混蛋。 :)你击败了我15秒。 –
啊,完全错过了那部分......我甚至都不知道还有另外一个包需要:)就是这样!谢谢! :) –
@HonzaKalfus请注意,只有当您希望能够绑定“OpenIdConnectRequest”参数时才需要此软件包。如果您更喜欢低级别的API,您仍然可以使用'HttpContext.GetOpenIdConnectRequest()'。 – Pinpoint