【问题标题】:c# asp.net Web API Owin Json Tokenc# asp.net Web API Owin Json Token
【发布时间】:2017-02-28 09:11:30
【问题描述】:

嘿,我刚刚在tutorial 之后编写了一个基于令牌的身份验证。 因此,只要我将 POST 请求作为 x-www-form-urlencoded 发送,一切都会好起来的。所以现在我的队友需要用一个json来获取令牌,但他得到的只是“不支持的grant_type”。那么我可以更改令牌的可接受类型还是必须找到其他解决方案?

我的配置如下:

public void Configuration(IAppBuilder app)
    {
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        var myProvider = new MyAuthorizationServerProvider();
        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = myProvider
        };

        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        }
    }

这就是我的请求的样子 请记住这不适用于 json 并且使用 JSON 它不起作用: 最好的问候:)

【问题讨论】:

  • 您是否在请求中将grant_type 作为password 发送..??
  • 在我的问题中添加了我的请求图片。是这样吗?
  • 是的,它是正确的...现在在您的标头中使用 access_token 验证您的 API 请求...
  • Eg: Authorization : bearer access_token...in the header of postman
  • 是的,我已经知道了,但问题是这只适用于 x-www-form-urlencoded 而不是 JSON

标签: c# asp.net json asp.net-web-api asp.net-identity


【解决方案1】:

使用 application/x-www-form-urlencoded 作为 Content-Type 的原因很简单:OAuth2 规范 (RFC 6749) 要求令牌请求使用此内容类型。

任何其他内容类型都会破坏符合 OAuth2 的客户端兼容性。我建议您不要更改此标准行为。

Microsoft.Owin.Security.OAuthOAuthAuthorizationServerMiddleware(更准确地说是内部使用的OAuthAuthorizationServerHandler)的默认实现只是忽略了Content-Type 标头并尝试将请求正文作为表单读取。

另辟蹊径 , 在 RequestBody 你可以写, grant_type=password&username=yourUserName&password=MyPassword123

还要确保在 grant_type=password&username=username&password=password 之后没有空格或换行符。

【讨论】:

  • 是的,现在为我工作了......对不起,我花了这么长时间^^
猜你喜欢
  • 2015-12-25
  • 2023-03-15
  • 2016-12-20
  • 1970-01-01
  • 2016-04-12
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
相关资源
最近更新 更多