【问题标题】:client_id is always null in ValidateClientAuthenticationclient_id 在 ValidateClientAuthentication 中始终为空
【发布时间】:2016-09-04 07:14:36
【问题描述】:

我正在尝试在 ValidateClientAuthentication 函数中验证客户端 ID。我正在使用 grant_type=password 流。这是我在身份验证令牌请求正文中传递 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 传递给令牌端点的正确方法是什么?

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    如果您的 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()

    【讨论】:

    • 如果我们尝试实现两个流程怎么办:密码和客户端凭据。对于密码,我们不需要 ClientID,因为它仅适用于具有实际用户的 Web 应用程序。 ClientCredentials 旨在供第三方用于 API。
    • 非常感谢您的帮助 - 很难找到有关 .net 框架 owin 的实际详细信息
    猜你喜欢
    • 2017-10-11
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2019-07-18
    • 2019-08-18
    • 2011-12-29
    相关资源
    最近更新 更多