# Discourse 作为 identity provider 在 localhost 不起作用

**URL:** https://meta.discourse.org/t/discourse-as-an-identity-provider-not-working-in-localhost/228885
**Category:** SSO
**Created:** [2022年六月2日 20:34 UTC](https://meta.discourse.org/t/discourse-as-an-identity-provider-not-working-in-localhost/228885 "2022-06-02T20:34:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Devarsh\_Mavani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/devarsh_mavani/32/251071_2.png) [@Devarsh\_Mavani](https://meta.discourse.org/u/Devarsh_Mavani)
#### Post date: [2022年六月2日 20:34 UTC](https://meta.discourse.org/t/discourse-as-an-identity-provider-not-working-in-localhost/228885/1 "2022-06-02T20:34:16Z")

</div>

我在本地设置了 discourse。现在我正尝试将 discourse 用作身份提供者。我遵循了本指南

> [@Use Discourse as an identity provider (SSO, DiscourseConnect)](https://meta.discourse.org/t/using-discourse-as-an-identity-provider-sso-discourseconnect/32974):
>
> So you want to use Discourse as an identity provider for your own web app? Great! Let’s get started. Enable [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) provider setting Under Discourse admin site settings (/admin/site\_settings) enable setting enable discourse connect provider and add a secret string to discourse connect provider secrets (used to hash SSO payloads). Implement [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) in your web app: Generate a random [nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce). Let’s call this value NONCE. Save it temporarily so that you can verify it with the …

但出现错误

```plaintext
GET http://localhost:3000/session/sso_provider?sso=bm9uY2U9MWc3NlQyaHRVZTZTYzJENjVEbzlzUT09JnJldHVybl9zc29fdXJsPWh0dHA6Ly9sb2NhbGhvc3Q6ODA4MS9yZWRpcmVjdA%3D%3D&sig=abcdb4a453080b7bac871d1d8d660bc731cfbc2a26fc6013f19a848c0018461c 422 (Unprocessable Entity)

```

注意：发起请求的客户端在 localhost 上，discourse 服务器也在 localhost 上。  
我尝试在网上查找类似的问题，但没有找到任何帮助。我还在下面附上了我的 node js 代码供参考

```javascript
app.get('/login', (req, res) => {
  let nonce = crypto.randomBytes(16).toString('base64');
  const payload = `nonce=${nonce}&return_sso_url=http://localhost:8081/redirect`
  const base64 = Buffer.from(payload).toString('base64')
  const URL_ENCODED_PAYLOAD = encodeURIComponent(base64)
  let hmac = crypto.createHmac('sha256', 'myrandomstring')
  hmac.update(URL_ENCODED_PAYLOAD)
  let HEX_SIGNATURE = hmac.digest('hex').toLowerCase()
  res.redirect(`http://localhost:3000/session/sso_provider?sso=${URL_ENCODED_PAYLOAD}&sig=${HEX_SIGNATURE}`)
})

```

有人能帮我弄清楚为什么它不起作用吗？谢谢。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2022年六月3日 00:03 UTC](https://meta.discourse.org/t/discourse-as-an-identity-provider-not-working-in-localhost/228885/2 "2022-06-03T00:03:14Z")

</div>

我相当确定它需要 https，而 https 在本地主机上不起作用。

---

<div class="post-metadata">

### Author: ![Devarsh\_Mavani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/devarsh_mavani/32/251071_2.png) [@Devarsh\_Mavani](https://meta.discourse.org/u/Devarsh_Mavani)
#### Post date: [2022年六月3日 21:08 UTC](https://meta.discourse.org/t/discourse-as-an-identity-provider-not-working-in-localhost/228885/3 "2022-06-03T21:08:32Z")

</div>

@pfaffman 它在本地主机上可以工作。原来是我这边的一个错误。但现在我遇到了一个问题：当我被重定向到 Discourse 页面时，我输入用户名和密码并点击登录，但它没有将我重定向回我的网站。我在这里读到了一篇类似的帖子 [https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/4，但他们已经修复了社交登录的问题（我想不是用户名密码认证）。你能帮我解决这个问题吗？谢谢。](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/4%EF%BC%8C%E4%BD%86%E4%BB%96%E4%BB%AC%E5%B7%B2%E7%BB%8F%E4%BF%AE%E5%A4%8D%E4%BA%86%E7%A4%BE%E4%BA%A4%E7%99%BB%E5%BD%95%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%88%E6%88%91%E6%83%B3%E4%B8%8D%E6%98%AF%E7%94%A8%E6%88%B7%E5%90%8D%E5%AF%86%E7%A0%81%E8%AE%A4%E8%AF%81%EF%BC%89%E3%80%82%E4%BD%A0%E8%83%BD%E5%B8%AE%E6%88%91%E8%A7%A3%E5%86%B3%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E5%90%97%EF%BC%9F%E8%B0%A2%E8%B0%A2%E3%80%82)
