How to Configure WeChat OAuth Authentication for Captive Portals
This technical guide explains how to configure WeChat OAuth authentication for captive portals. It details the required platform registrations, OAuth 2.0 flow, scope selection, and network enforcement mechanisms necessary to capture first-party data from Chinese visitors securely.
Listen to this guide
View podcast transcript

执行摘要
当中国访客连接到您的 WiFi 时,如果登录页面仅提供电子邮件或 Facebook 登录,会立即产生使用阻碍。微信拥有 13.8 亿月活跃用户,将其配置为身份提供商可以消除这一障碍。本指南将阐述如何为 Captive Portal 实现微信 OAuth 2.0 认证,详细介绍必要的平台注册、OAuth 流程以及将成功登录转化为网络访问所需的网络强制执行机制。我们将涵盖企业级硬件的技术实现,以及 GDPR 和《个人信息保护法》(PIPL)下的合规要求。
技术架构
Captive Portal 会拦截来自未认证设备的 HTTP 流量,并将其重定向到托管在门户服务器上的登录页面。当您集成微信 OAuth 时,即是在此流程中插入了一个第三方身份提供商。

具体交互步骤如下:
- 访客连接到 SSID。
- 无线接入点(AP)或无线控制器检测到缺乏已认证的会话,并将 HTTP 流量重定向到 Captive Portal URL。
- 访客选择微信登录。
- 门户服务器将浏览器重定向到微信的授权端点(
open.weixin.qq.com),并传递AppID、redirect_uri、response_type=code和scope。 - 微信处理身份验证。如果访客在微信内置浏览器中使用
snsapi_base作用域,此过程将静默进行。 - 微信携带临时授权码重定向回门户的
redirect_uri。 - 门户服务器通过调用
api.weixin.qq.com/sns/oauth2/access_token,用该授权码换取访问令牌。 - 微信返回
access_token、refresh_token以及用户的openid。
平台注册要求
实现微信登录需要在正确的开发者平台上进行注册。微信运营着两个不同的平台,选择错误的平台会导致集成失败。
微信公众平台
对于在微信内置浏览器中为访客提供服务的 Captive Portal,您需要在微信公众平台(mp.weixin.qq.com)上注册一个服务号。订阅号缺少必要的 OAuth 网页授权权限。服务号同时支持 snsapi_base 和 snsapi_userinfo 作用域。
微信开放平台
对于从微信外部的标准移动浏览器(例如 Android 上的 Chrome 或 iOS 上的 Safari)访问的 Captive Portal,您需要一个在开放平台(open.weixin.qq.com)注册的网站应用。这使用 snsapi_login 作用域,并呈现一个供用户使用其微信应用扫描的二维码。
大多数企业部署都需要进行这两种注册,以覆盖所有访问方式。
作用域选择与数据收集
作用域参数决定了微信返回给您门户服务器的数据。这一决定会同时影响用户摩擦和数据隐私合规性。

snsapi_base
此作用域仅返回 OpenID,即您公众号内用户的唯一标识符。它不需要用户授权提示,从而使身份验证对用户无感。这对于您已拥有其个人资料的回访访客,或者对于将零摩擦置于新数据收集之上的场所来说是最佳选择。
snsapi_userinfo
此作用域返回 OpenID 以及用户的微信昵称、头像、性别、语言设置和城市。它需要一个明确的授权页面,从而引入了摩擦。在需要建立个人资料的首次访客注册中,请使用此作用域,并配合符合 GDPR 的授权层。
网络强制执行集成
获取 OAuth 令牌可以证明身份,但它并不能打开网络。您必须使用标准协议将成功的身份验证转化为网络访问。
RADIUS 授权变更 (CoA)
在 IEEE 802.1X 和 RFC 3576 中定义的 RADIUS CoA 允许门户服务器在 OAuth 成功后向网络控制器发送请求。然后,控制器将设备从未经身份验证的 VLAN 移动到访客 VLAN。这是包括 Cisco Meraki、HPE Aruba、Ruckus 和 Juniper Mist 在内的企业级硬件的标准配置。
MAC 地址旁路
或者,门户服务器将设备的 MAC 地址注册为已授权客户端,然后控制器允许其访问。虽然实现起来更简单,但由于 MAC 地址可以被伪造,因此安全性较低。
Purple 的云覆盖技术可自动完成此转换,在微信 OAuth 完成后向底层硬件(包括 Ubiquiti UniFi、Cambium、Extreme 和 Fortinet)发送相应的信号。
合规与安全考量
GDPR 与 PIPL 对齐
如果您为欧洲访客提供服务,GDPR 适用于通过微信 OAuth 收集的数据。如果您为中国访客提供服务,则适用中国《个人信息保护法》(PIPL)。这两个框架都要求处理具有合法基础、明确的目的限制和数据最小化。相比 snsapi_userinfo,snsapi_base 作用域更容易符合数据最小化原则。
CSRF 防护
OAuth 请求中的 state 参数可防止跨站请求伪造。您必须生成一个加密随机的 state 值,将其存储在用户会话中,并在微信重定向返回时对其进行验证。
重定向 URI 验证
微信会根据在平台上注册的授权域名验证 redirect_uri。如果您的门户服务器使用不同的子域名、路径或使用 HTTP 代替 HTTPS,则 OAuth 流程将失败并报错 40029。
有关保护网络的更多信息,请参阅我们的 Enterprise WiFi Security: A Complete Guide for 2026 。
Key Definitions
snsapi_base
A WeChat OAuth scope that returns only the user's OpenID without displaying a consent prompt.
Used when IT teams need to authenticate returning visitors silently without causing login friction.
snsapi_userinfo
A WeChat OAuth scope that returns the OpenID along with demographic data (nickname, gender, city) and requires explicit user consent.
Used during first-time registration when marketing teams need to build a visitor profile.
OpenID
A unique identifier for a specific user within a specific WeChat Official Account.
Used as the primary key in the portal database to track visitor behaviour and return visits.
RADIUS CoA
Change of Authorisation. A mechanism defined in RFC 3576 that allows a server to modify the authorisation state of an active session.
Used by the portal server to tell the wireless controller to grant network access after successful WeChat authentication.
PIPL
Personal Information Protection Law. China's comprehensive data privacy regulation.
Must be considered alongside GDPR when designing the consent flow for Chinese visitors using WeChat login.
AppID and AppSecret
The credentials provided by WeChat to identify and authenticate your application.
The AppSecret must remain securely on the portal server and never be exposed in client-side code.
State Parameter
A cryptographically random string passed in the OAuth request and validated upon return.
Essential for preventing Cross-Site Request Forgery (CSRF) attacks on the captive portal.
MAC Address Bypass
A method of granting network access by authorising the device's hardware address rather than requiring 802.1X authentication.
An alternative to RADIUS CoA for simpler network setups, though less secure.
Worked Examples
A luxury retail brand in London wants to offer WeChat login for Chinese shoppers. They want to collect demographic data to understand their customer base, but they are concerned about GDPR compliance and high drop-off rates at the portal.
The retailer should register a Service Account on the WeChat Official Accounts Platform. They must configure the portal to use the snsapi_userinfo scope for first-time connections to gather demographic data (nickname, gender, city). To ensure GDPR compliance, the portal page must display a clear, conscious-choice opt-in before the WeChat redirect, explaining exactly what data is collected and why. For returning shoppers, the portal should detect the MAC address and use snsapi_base for silent re-authentication, minimising friction.
A stadium deploys a new WiFi network using HPE Aruba controllers. They have configured WeChat OAuth, and the portal successfully receives the access token, but the visitor's device remains on the captive portal page and cannot access the internet.
The integration lacks a network enforcement mechanism. The portal server has verified the user's identity with WeChat, but it has not instructed the HPE Aruba controller to grant access. The portal server must be configured to send a RADIUS Change of Authorisation (CoA) message to the controller, instructing it to transition the user's MAC address from the pre-authentication role to the authenticated guest role.
Practice Questions
Q1. You are deploying a captive portal across a retail chain. Testing shows that users opening the portal in Safari on iOS receive an error when selecting WeChat login, but users opening the portal from within a WeChat message link authenticate successfully. What is the likely cause?
Hint: Consider the difference between the WeChat in-app browser and standard mobile browsers.
View model answer
The implementation is likely relying solely on a Service Account registered on the Official Accounts Platform, which only supports OAuth within the WeChat in-app browser. To support Safari on iOS, you must also register a Website Application on the WeChat Open Platform and implement user agent detection to route Safari users to the QR code flow.
Q2. Your portal server logs show frequent 40029 'invalid code' errors returning from the WeChat API during the access token exchange. What configuration should you check first?
Hint: Think about how WeChat validates the source of the authentication request.
View model answer
You should verify the redirect_uri configuration. WeChat strictly validates the redirect URI against the authorised domain registered in the developer console. If the portal is using a different subdomain, or if it drops HTTPS, WeChat will reject the code exchange.
Q3. A venue operator wants to collect visitor data but insists on zero friction during the login process. They request that you configure WeChat login to collect the visitor's nickname and city without showing a consent prompt. How do you respond?
Hint: Review the capabilities of the different OAuth scopes.
View model answer
You must inform the operator that this is technically impossible. Collecting demographic data like nickname and city requires the snsapi_userinfo scope, which mandatorily triggers a WeChat consent prompt. To achieve zero friction, you must use snsapi_base, which operates silently but only returns the OpenID.
Continue reading in this series
Designing B2B Captive Portals: Collecting Registered Name and Company Data
This guide provides IT managers and venue operators with a vendor-neutral technical framework for designing B2B captive portals. It details how to structure registration fields to capture registered name and company data, ensuring high completion rates while maintaining GDPR compliance and building account-level intelligence.
Designing B2B Captive Portals: Collecting Registered Name and Company Data
This guide provides IT managers and venue operators with a vendor-neutral technical framework for designing B2B captive portals. It details how to structure registration fields to capture registered name and company data, ensuring high completion rates while maintaining GDPR compliance and building account-level intelligence.
Captive Portal Architecture: Security, Redirection, and Best Practices
A definitive technical reference on enterprise captive portal architecture. This guide unpacks network isolation, DNS redirection, RADIUS authentication, and security compliance for IT leaders deploying secure, data-rich guest WiFi networks.