oauth2端点上的预检没有正文并返回不受支持的授权类型

时间:2017-11-24 17:06:31

标签: asp.net http oauth-2.0 owin

我的asp.net owin api有以下中间件来定义oauth2 tken端点。

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
            {
                AllowInsecureHttp = _env.IsDevelopment,
                TokenEndpointPath = new PathString("/oauth2/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(24*60),
                Provider = _oAuthAuthorizationServerProvider,
                AccessTokenFormat = new CustomJwtFormat(_issuer, _secret)
            });

尝试登录我的网络应用时,http发布请求会在浏览器启动的http选项预检请求之后发生。

但是,选项请求失败,错误消息“unsupported_grant_type”,因为浏览器发起的此预检请求没有正文。

1 个答案:

答案 0 :(得分:0)

根据post,解决方案是覆盖MatchEndpoint派生类中的OAuthAuthorizationServerProvider

public override Task MatchEndpoint(OAuthMatchEndpointContext context)
        {
            if (context.OwinContext.Request.Method == "OPTIONS" && context.IsTokenEndpoint)
            {
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "POST" });
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "accept", "authorization", "content-type" });
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
                context.OwinContext.Response.StatusCode = 200;
                context.RequestCompleted();

                return Task.FromResult<object>(null);
            }

            return base.MatchEndpoint(context);
        }