c#asp.net Web API Owin Json令牌
嘿,我刚刚编程了一个基于令牌的身份验证。 因此,只要我以x-www-form-urlencoded的形式发送我的POST请求,一切都会顺利进行。所以现在我的队友需要用json获取令牌,但他得到的只是“不支持的grant_类型”。因此,我可以更改令牌的可接受类型,还是必须找到其他解决方案 我的配置如下所示:c#asp.net Web API Owin Json令牌,c#,asp.net,json,asp.net-web-api,asp.net-identity,C#,Asp.net,Json,Asp.net Web Api,Asp.net Identity,嘿,我刚刚编程了一个基于令牌的身份验证。 因此,只要我以x-www-form-urlencoded的形式发送我的POST请求,一切都会顺利进行。所以现在我的队友需要用json获取令牌,但他得到的只是“不支持的grant_类型”。因此,我可以更改令牌的可接受类型,还是必须找到其他解决方案 我的配置如下所示: public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOpt
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,它不起作用:
致意:)使用application/x-www-form-urlencoded as Content Type的原因很简单:OAuth2规范(RFC 6749)要求令牌请求使用此内容类型 任何其他内容类型都将破坏与OAuth2兼容的客户端兼容性。我建议你不要改变这种标准行为
Microsoft.Owin.Security.OAuth
中的OAuthAuthorizationServerMiddleware
(更确切地说是内部使用的)的默认实现只会忽略内容类型
头,并尝试将请求正文作为表单读取
换一种方式
,您可以在RequestBody中编写,
授权类型=密码和用户名=您的用户名和密码=我的密码123
另外,请确保在授予\ u type=password&username=username&password=password之后没有空格或换行符
您是否在您的请求中以
密码的形式发送grant\u type
。在我的问题中添加了我请求的图片。是这样吗?是的,它是正确的…现在在你的头中使用访问令牌验证你的API请求…例如:授权:承载访问令牌…在postmanyeah的头中我已经得到了这个,但问题是,这只适用于x-www-form-urlencoded,而不适用于现在为我工作的JSONyeah。。。对不起,我花了这么长时间^^