c# - client_id 在 ValidateClientAuthentication 中始终为 null

标签 c# asp.net-web-api oauth-2.0 owin

我正在尝试在 ValidateClientAuthentication 函数中验证客户端 ID。我正在使用 grant_type=password 流程。以下是我在身份验证 token 请求正文中传递 client_id 的方式。

Content-Type: application/x-www-form-urlencoded
grant_type=password&username=user1@test.com&password=pwduser1&client_id=B36B-07DEAC802BEA

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

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");
   }

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

感谢您的帮助。

最佳答案

如果您的 client_id 作为表单参数传递,您必须通过 context.TryGetFormCredentials(out clientId, out clientSecret); 获取它>

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

从您的请求中获得client_id 后,执行context.Validated(clientId),这将设置您的context.ClientId属性,此属性将始终为 null,直到您完成 context.Validated()

关于c# - client_id 在 ValidateClientAuthentication 中始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119386/

相关文章:

c# - 使用 TinyIoC 解析带有构造函数参数的具体类型

C#:产生连续的音调直到被打断

c# - 通过 C# 代码更新 web.config

r - 通过 API 调用获取文件(R & 管道工)

c# - IDX :20803 Unable to obtain configuration from. .. - Web API

php - oAuth 2.0 用户与客户端?

c# - 测试订阅 EventAggregator

c# - 如何使用过滤器 HostAuthenticationFilter 修改 "Authorization has been denied for this request."

javascript - OAuth2 隐式授予(带弹出窗口,无需 LocalStorage)

java - 如何在 Spring Boot 微服务应用程序之间传递用户 ID