c# - 如何使用服务器端代码从 http post header 获取表单数据
问题描述
我收到来自 MS 登录的 azure Active Directory 的回调。帖子在标题中包含一个 id 令牌,需要将其读入服务器端的字符串变量。
我可以在 network/headers/formdata 下的 chrome 开发工具中看到数据。我已经遍历了标题集合 - 它不存在。你到底是如何访问这些数据的?
用于显示标题的代码(实际上使用 vb.net,但现在它只占市场的一小部分,因为我正在为 c# 发布,如果需要,我可以很容易地翻译它。):
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection
' Load Header collection into NameValueCollection object.
coll = Request.Headers
' Put the names of all keys into a string array.
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
txtOutput.Text += "Key: " & arr1(loop1) & vbCrLf
arr2 = coll.GetValues(loop1)
' Get all values under this key.
For loop2 = 0 To arr2.GetUpperBound(0)
txtOutput.Text += "Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & vbCrLf & vbCrLf
Next loop2
Next loop1
我期待在 headers 集合中找到这些数据,但它不存在。
解决方案
你几乎拥有它。答案就在你的问题中。使用coll = Request.Form
而不是coll = Request.Headers
.
推荐阅读
- mongodb - 用于匹配的 Mongo 聚合,其中两个数组值之间的差异在 X 和 Y 之间
- html - Bootstrap4 Modal Body 不让 div 拉伸 100% 高度
- java - 为什么从第一个字符串数组解析数据时抛出异常,但跳过第一个数组时却没有?
- python - 生成多个敌人 pygame
- kubernetes - kubernetes pod 容器通过 CrashLoopBackoff 不断重启
- database - 如何使用 MongoDB 实现 SELECT ... FOR UPDATE 和 Transactions 水平扩展服务?
- javascript - 每次更新状态时如何渲染元素
- visual-studio-code - VSCode 不会为 nextjs (9.5.2) 绑定客户端断点
- reactjs - 我想在下拉菜单上显示标题,当有人选择一个选项并提交时,我想提交年份值
- sql - BigQuery - 连接列的所有行,直到达到特定值