在ValidateClientAuthentication中,client_id始终为null

Shi*_*ath 7 c# oauth-2.0 asp.net-web-api owin

我正在尝试验证ValidateClientAuthentication函数中的客户端ID.我正在使用grant_type =密码流.以下是我在auth令牌请求正文中传递client_id的方法.

Content-Type: application/x-www-form-urlencoded
grant_type=password&username=user1@test.com&password=pwduser1&client_id=B36B-07DEAC802BEA
Run Code Online (Sandbox Code Playgroud)

当我尝试访问ClientId以验证客户端ID时,它始终为null.

if (context.ClientId != null) 
   {
       //Set the client who initiated request is valid
       context.Validated();
   }
   else
   {
      //reject access to application
      context.Rejected();
      context.SetError("Invalid client_id", "Client_id must present in the request header");
   }
Run Code Online (Sandbox Code Playgroud)

当grant_type = password时,将客户端ID传递给令牌端点的正确方法是什么?

感谢您的帮助.

Duy*_*Duy 16

如果你client_id作为一个表格参数传递,你必须通过这样做context.TryGetFormCredentials(out clientId, out clientSecret);

如果您client_id作为Authorization标题传递,您可以通过执行它context.TryGetBasicCredentials(out clientId, out clientSecret);

一旦你client_id从你的请求得到了,那么context.Validated(clientId),这将设置你的context.ClientId属性,这个属性将永远为空,直到你完成context.Validated()