gpt4 book ai didi

python - 使用 IIS HTTP PlatformHandler 前置并使用 Windows 身份验证时,如何在 Python 中获取经过身份验证的用户名?

转载 作者:行者123 更新时间:2023-12-04 09:31:52 24 4
gpt4 key购买 nike

HttpPlatformHandler 通过启用 支持转发身份验证 token forwardWindowsAuthToken 在 web.config 中设置。
当需要使用 Windows 集成身份验证时,这听起来像是一个有用的功能。
document关于这一点非常模糊,并没有解释如何使用此 token 来获取经过身份验证的用户名。

If this setting is set to true, the token will be forwarded to thechild process listening on %HTTP_PLATFORM_PORT% as a header'X-IIS-WindowsAuthToken' per request. It is the responsibility of thatprocess to call CloseHandle on this token per request. The defaultvalue is false.


在我的用例中,我需要将 Windows 集成身份验证与 Python 一起使用,因此使用 IIS 前端进行设置并使用 HTTP 平台处理程序将请求转发到 Python。
问题是,如何从 Python 中提供的 token 中获取用户名?
'X-IIS-WindowsAuthToken' header 中的 token 看起来像 22b 之类的 3 个字符的十六进制。

最佳答案

好的,所以我对此进行了一些研究并最终回顾了 Microsoft.AspNetCore.Server.IISIntegrateion.AuthenticationHandler做到了。
然后在想出一种方法之后,我想发布这个答案,以便 1) 我以后可以找到它,2) 至少它在 SO 上,以防其他人想知道。
好的,所以十六进制值是句柄,我们可以使用句柄调用模拟用户,然后获取用户名,完成。
您只需要 pywin32包裹:pip install pywin32Python 中的完整示例:

import win32api
import win32security
if 'x-iis-windowsauthtoken' in request.headers.keys():
handle_str = request.headers['x-iis-windowsauthtoken']
handle = int(handle_str, 16) # need to convert from Hex / base 16
win32security.ImpersonateLoggedOnUser(handle)
user = win32api.GetUserName()
win32security.RevertToSelf() # undo impersonation
win32api.CloseHandle(handle) # don't leak resources, need to close the handle!
print(f"user name: {user}")


关于python - 使用 IIS HTTP PlatformHandler 前置并使用 Windows 身份验证时,如何在 Python 中获取经过身份验证的用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62806295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com