跳到主内容

添加微信作为 OAuth 提供商

本指南介绍如何让用户使用微信登录 Casdoor:在 PC 上使用微信应用扫描二维码,以及在手机上使用微信内置浏览器登录。


学习目标​

  • 从微信开放平台和微信公众平台获取凭证。
  • 为 PC 和移动端添加微信提供商。
  • 在登录页面显示微信选项卡。

所需条件​

  • 在微信开放平台上已审核通过的应用,用于 PC 端登录
  • 用于在微信内登录:一个微信公众平台的公众号
  • Casdoor 管理控制台的管理员访问权限

关于微信登录​

Sub typeWhere users sign inCredentials
Web (default)In a PC browser, by scanning a QR code with the WeChat appClient ID and Client secret from the WeChat Open Platform
移动端In the built-in browser of the WeChat appClient ID 2 and Client secret 2 from the WeChat Media Platform, and the Access token that you set in the server configuration of the official account

微信不允许从其他应用或微信外的移动浏览器进行登录。为了同时支持 PC 和微信,请创建两个微信提供商,分别设置不同的子类型。

在微信内置浏览器中,Casdoor 始终通过公众号进行登录,使用 Client ID 2。如果提供商没有配置 Client ID 2,Casdoor 将不会启动登录流程,并显示以下提示:Please open the login page in an external browser to sign in with WeChat, or ask the administrator to configure a WeChat Official Account。

提示

在开放平台上关联你的微信开放平台账号和微信公众平台账号。这样,微信会在 PC 端和微信内为同一用户提供相同的身份标识。

在 Casdoor 中添加提供商​

  1. 在微信开放平台上,获取已审核通过应用的 App ID 和 App Secret。

    WeChat Open Platform application

  2. 在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。

  3. 将 Category 设置为 OAuth,Type 设置为 WeChat,并将 Sub type 设置为 Web 或 Mobile。

  4. 输入该子类型的凭证,如上文表格所示。

  5. 如果你设置了这两组凭证以及访问令牌,可以开启 Use WeChat Media Platform in PC。这样,PC 用户在扫描二维码之前也可以被提示关注公众号。此功能仅适用于 PC,因为手机无法扫描自身的二维码。

  6. 保存提供商.

在登录页面显示微信​

  1. 打开应用的编辑页面。

  2. 在 Providers 选项卡中添加微信提供商。

  3. 将 WeChat 添加到 Signin methods。

  4. 保存应用。

    WeChat in the Signin methods

现在登录页面包含一个微信选项卡:

  1. 用户选择微信选项卡。页面显示一个二维码。
  2. 用户使用微信应用扫描二维码并授权登录。
  3. 如果二维码过期,用户点击下方的刷新按钮。

WeChat tab on the sign-in page

另请参阅​