正在构建一个自托管 Django Web 应用程序,该应用程序将使用身份https://github.com/rayluo/identity(MSAL 上的包装器库)来获取 OAuth 令牌,以便访问我的租户的资源(通过 MSGraph API 调用)
我已从此处下载了示例 Web 应用程序https://github.com/Azure-Samples/ms-identity-python-webapp-django,根据使用说明在我的 AzureAd 租户上授权了该应用程序
http://localhost:8080/redirect
作为重定向 URI 。
Launching the app on my localhost I get the follwoing error:
Django version 5.0.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.
Session data corrupted
redirect_uri mismatch: configured = http://localhost:8080/redirect, calculated = http://127.0.0.1:8080/redirect
[26/Mar/2024 10:53:19] "GET / HTTP/1.1" 200 715
尝试将 Azure AD 上的 URI 更改为:http://127.0.0.1:8080/redirect 但显然:
尝试通过 ngrok 在 AzureAD 和我的本地主机之间建立 http 隧道。服务器对登录页面响应良好:
但是一旦加载,我就已经在服务器日志中看到 URI 再次不匹配:
Session data corrupted
redirect_uri mismatch: configured = https://6ae1-89-64-xx-xx.ngrok-free.app/redirect, calculated = http://6ae1-89-64-xx-xx.ngrok-free.app/redirect
[26/Mar/2024 10:34:47] "GET / HTTP/1.1" 200 730
Not Found: /favicon.ico
请注意,在上面的日志消息中,分配给“计算”变量的 URI 已删除其“s”!
Python 3.10.5,
封装版本:
Django==5.0.3
identity==0.6.0
msal==1.28.0
http://localhost:8080/redirect!=http://127.0.0.1:8080/redirect?这是一个错误还是这背后有我不知道的想法?
MSAL 在我的重定向 URI 中的 https 中删除 "s" 的位置和原因?
如何解决这个问题?
我使用相同的GitHub代码并成功登录和注销,没有任何问题。
为了解决该问题,我使用以下命令来运行该应用程序。
python manage.py runserver localhost:8000
.env:
POST_BUILD_COMMAND=python manage.py migrate
CLIENT_ID='<client_ID>'
AUTHORITY='https://<tenant_name>.b2clogin.com/<tenant_name>.onmicrosoft.com/<policy_name>'
REDIRECT_URI='http://localhost:8000/redirect'
SCOPE=User.ReadBasic.All
ENDPOINT=https://graph.microsoft.com/v1.0/users
SIGNUPSIGNIN_USER_FLOW='<policy_name>'
EDITPROFILE_USER_FLOW='<policy_name>'
RESETPASSWORD_USER_FLOW='<policy_name>'
我在 Azure AD B2C 身份验证中添加了以下 URL 作为单页应用程序,如下所示:
http://localhost:8000/redirect
输出:
Django项目运行成功如下:
C:\Users\xxxxxxxx\Downloads\ms-identity-python-webapp-django-main\ms-identity-python-webapp-django-main>python manage.py runserver localhost:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
March 27, 2024 - 16:45:07
Django version 5.0.3, using settings 'mysite.settings'
Starting development server at http://localhost:8000/
Quit the server with CTRL-BREAK.
Session data corrupted
[27/Mar/2024 16:45:27] "GET / HTTP/1.1" 200 710
[27/Mar/2024 16:46:33] "GET /redirect?state=BErLfztOxxxxxxxxxxx&client_info=eyJ1aWQixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx HTTP/1.1" 302 0
[27/Mar/2024 16:46:33] "GET / HTTP/1.1" 200 759
[27/Mar/2024 16:46:52] "GET /logout HTTP/1.1" 302 0
[27/Mar/2024 16:46:53] "GET / HTTP/1.1" 200 710
浏览器输出:
我使用上述 URL 获得了以下输出,并单击 Sign In 按钮进行登录,如下所示:
它将我重定向到以下页面,我提供了用于登录的 B2C 凭据,如下所示:
我成功登录并看到以下页面。我点击 Logout 按钮注销,如下所示。