Okta - Public 应用程序使用 VB.Net 的主要身份验证

Okta - Primary Authentication with Public Application using VB.Net

使用 API 文档,我们正在尝试使用它来进行身份验证: Public 应用程序的主要身份验证:

http://developer.okta.com/docs/api/resources/authn.html#primary-authentication-with-public-application: 通过 public 应用程序

使用 username/password 凭据对用户进行身份验证

请求示例

curl -v -X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
  "username": "dade.murphy@example.com",
  "password": "correcthorsebatterystaple",
  "relayState": "/myapp/some/deep/link/i/want/to/return/to",
  "options": {
    "multiOptionalFactorEnroll": false,
    "warnBeforePasswordExpired": false
  }
}'

在 VB.Net 中尝试这个,我们得到:

Dim request As WebRequest = WebRequest.Create("https://dev-XXX.oktapreview.com/api/v1/authn")
request.Credentials = New NetworkCredential(.UserName, .Password)
request.ContentType = "application/json"
request.Method = "POST"

Dim response As WebResponse = request.GetResponse()

当我们得到响应时,它给出了 "The remote server returned an error: (400) Bad Request." 错误并且没有更多有用的信息。

显然我们想要做的是使用正确格式的参数和 return 我们可以查询成功响应的 WebResponse 来执行 WebRequest:

{
  "expiresAt": "2015-11-03T10:15:57.000Z",
  "status": "SUCCESS",
  "relayState": "/myapp/some/deep/link/i/want/to/return/to",
  "sessionToken": "00Fpzf4en68pCXTsMjcX8JPMctzN2Wiw4LDOBL_9pe",
  "_embedded": {
    "user": {
      "id": "00ub0oNGTSWTBKOLGLNR",
      "passwordChanged": "2015-09-08T20:14:45.000Z",
      "profile": {
        "login": "dade.murphy@example.com",
        "firstName": "Dade",
        "lastName": "Murphy",
        "locale": "en_US",
        "timeZone": "America/Los_Angeles"
      }
    }
  }
}

提前致谢!

您正在为请求设置凭据,而不是发送 JSON 负载。

看看这个例子: http://dotnetpad.com/7ri4979f

基于 this question and answer.