servicestack,http-status-code-401,.net,Postman,servicestack,Http Status Code 401" /> servicestack,http-status-code-401,.net,Postman,servicestack,Http Status Code 401" />

.net ServiceStack BasicAuth返回401和客户端\u凭据

.net ServiceStack BasicAuth返回401和客户端\u凭据,.net,postman,servicestack,http-status-code-401,.net,Postman,servicestack,Http Status Code 401,我有一个远程端点,它需要grant_类型的基本身份验证和客户端_凭据 在《邮递员》中,我可以看到标题和正文如下: Request Headers: Content-Type: application/x-www-form-urlencoded Authorization: Basic <my Base64 encoded credentials> User-Agent: PostmanRuntime/7.15.2 Accept: "*/*" Cache-Contr

我有一个远程端点,它需要grant_类型的基本身份验证和客户端_凭据

在《邮递员》中,我可以看到标题和正文如下:

Request Headers:
  Content-Type: application/x-www-form-urlencoded
  Authorization: Basic <my Base64 encoded credentials>
  User-Agent: PostmanRuntime/7.15.2
  Accept: "*/*"
  Cache-Control: no-cache
  Postman-Token: <uuid>
  Host: <target url>
  Accept-Encoding: gzip, deflate
  Content-Length: 29
  Connection: keep-alive

Request Body:
  grant_type=client_credentials

我还得到了401个未经授权的异常,这意味着grant\u类型的客户端\u凭据没有正确发布。在代码中解决这个问题需要什么?

我很快意识到主体需要是名称-值对,所以这就是复制邮递员发送的内容所需要的:

var response = url.PostToUrl(
  formData: new { grant_type = "client_credentials" }, 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });
var response = url.PostToUrl(
  formData: new { grant_type = "client_credentials" }, 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });