【问题标题】:unable to read values from Request.Params in c#无法从 C# 中的 Request.Params 读取值
【发布时间】:2015-10-14 08:57:07
【问题描述】:

我有以下代码让我无法从Request.Params 读取值。现在我只想读取我从发送者传递的值(在接收者中),即用户名和 SAMLResponse。

发件人

protected void Button1_Click(object sender, EventArgs e)
{
    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("MY URL");
    httpWReq.Method = "Post";
    XElement obj = XElement.Load(@"Load.xml");
    StringBuilder postData = new StringBuilder();
    postData = postData.Append("username=user&SAMLResponse=").Append(obj.ToString());                

    byte[] data = Encoding.UTF8.GetBytes(postData.ToString());

    httpWReq.Method = "POST";
    httpWReq.ContentType = "text/xml;encoding='utf-8'";
    httpWReq.ContentLength = data.Length;

    using (Stream stream = httpWReq.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

    string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}

接收者

public ActionResult LoginSSO()
{
    string rawSamlData, WindowName;
    SSOModel objSSOModel = new SSOModel();
    string str = Request.Params["username"];
    str = Request.Params["SAMLResponse"];
    ...
    ..
}

错误

任何想法,出了什么问题?

【问题讨论】:

  • string str = Request.Querystring["username"];

标签: c# httpwebrequest saml saml-2.0


【解决方案1】:

你能改变这一行吗:

httpWReq.ContentType = "text/xml;encoding='utf-8'";

到这里:

httpWReq.ContentType = "application/x-www-form-urlencoded";

然后给我们一个结果?

https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx

【讨论】:

  • 我之前尝试过,但没有成功。结果保持不变。因此,我也发布了 XML 内容,然后我切换到了你的建议。但没有运气
  • 然后尝试application/xml 而不是text/xml stackoverflow.com/questions/5653743/…
【解决方案2】:

我发现您的代码存在一些问题。

  1. 编码 - 当您执行obj.ToString() 时,您应该对 XML 文件进行编码,因为它可能包含会混淆的字符。
  2. 内容类型 - 您告诉接收者您正在发送 XML,但您实际上是在发送表单。所以你应该将内容类型设置为application/x-www-form-urlencoded

【讨论】:

  • 我将 postData 编码为整体。对于内容类型,我之前尝试过,但没有成功。然后我切换到我现在拥有的。
  • 您可能需要HttpUtility.UrlEncode您的帖子数据,然后才能将其写入流。
【解决方案3】:

这适用于我的应用程序。

  1. 正如其他回答者所说,将 Sender 中的 content-type 更改为 "application/x-www-form-urlencoded"

  2. Receiver中将string str = Request.Params["username"];更改为string str = Request.Form["username"];string str = Request["username"];

我希望它们也能在您的应用程序中工作。

【讨论】:

  • 您尝试过使用 XML 内容吗?
  • @AmneshGoel 不,因为这对我有用。这不适合你吗?
  • 我必须通过 XML 内容。用普通内容进行测试有什么意义?
  • 如果您在str = Request.Params["SAMLResponse"]; 收到System.Web.HttpRequestValidationException,请尝试Request.Unvalidated.Form["SAMLResponse"]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 2012-01-06
  • 2021-08-27
  • 2022-07-02
  • 2020-09-03
  • 1970-01-01
相关资源
最近更新 更多