2017-02-28 130 views
2

嘿,我刚刚编程了一个基于令牌的认证,跟在这个tutorial之后。 所以一切都很好,只要我发送我的POST请求为x-www-form-urlencoded。所以现在我的队友需要使用json获得令牌,但他得到的只是“不受支持的grant_type”。那么我可以更改令牌的可接受类型,还是必须找到另一种解决方案?c#asp.net Web API Owin Json令牌

我的配置是这样的:

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 This is how my request look like keep in mind this doesnt work with json 并配有JSON它不工作: enter image description here 最好的问候:)

+0

你发送'grant_type'作为您的要求'password' ..? –

+0

在我的问题中添加了我的请求的图片。这是对的吗? –

+0

是的,这是正确的...现在使用您的头中的access_token验证您的API请求... –

回答

1

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

任何其他内容类型都会打破OAuth2兼容客户端的兼容性。我建议你不要改变这种标准行为。

OAuthAuthorizationServerMiddleware默认实现(更精确地在内部使用OAuthAuthorizationServerHandler)从Microsoft.Owin.Security.OAuth只是忽略Content-Type头并试图无论如何读取请求体作为一种形式。

另一种方式 ,在RequestBody你可以写, grant_type =密码&用户名= yourUserName &密码= MyPassword123

另外,还要grant_type后确认=密码&用户名=用户名&密码=密码没有空格或换行符。

enter image description here

+1

是啊现在为我工作...抱歉,它花了我这么久^^ –