我需要在HTML中模仿一个简单的动作,这个HTML实际上是为了我的目的,但我必须在MVC ASP.NET中做同样的动作。
这是HTML:
<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM method="post" autocomplete="off" ACTION="https://<target IP>/auth/index.html/u">
Username:<BR>
<INPUT type="text" name="user" accesskey="u" SIZE="25" VALUE="">
<BR>
Password:<BR>
<INPUT type="password" name="password" accesskey="p" SIZE="25"
VALUE="">
<BR>
<INPUT type="submit">
</FORM>
</BODY>
此HTML获取两个参数“user”和“password”,并将它们发布到URI。我在我的应用程序中尝试了“GET”和“POST”方法,但它没有用。 这是我在MVC ASP.NET中的控制器:
[AllowAnonymous]
[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
string ArubaPost = "user=" + model.user + "&password=" + model.password;
string ArubaURI = "https://<target IP>/auth/index.html/u";
// this is to bypass the SSL certiface, not sure if it is needed
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
// web request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ArubaURI);
request.KeepAlive = false;
request.AllowAutoRedirect = false;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] postBytes = Encoding.Default.GetBytes(ArubaPost);
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
string location = response.Headers[HttpResponseHeader.Location];
return Redirect(ArubaURI + location);
}
// If we got this far, something failed, redisplay form
return View(model);
}
我也试过了一个“GET”动作,但是结果是一样的。我实际上没有任何错误,当它在浏览器中运行时,我可以看到使用Chrome检查元素发送状态代码为200的请求。 这是一个网络控制器重定向所有流量的页面,然后用户必须输入用户名和密码。然后,当它通过POST或GET发送时,网络控制器必须捕获参数并验证它们。在验证成功的情况下,网络控制器将用户重定向到他最初请求的页面,以便他可以在网上冲浪。这个简单的HTML页面实现了所有这些,但在MVC应用程序中,我被一次又一次地重定向到同一个登录页面。 任何想法可能是错的?
答案 0 :(得分:1)
它不起作用,因为身份验证cookie未存储在浏览器中。而是将它传递给您的服务器(MVC应用程序)。
使用您当前的方法无法实现目标。因为即使您的服务器将这些cookie提供给客户端(浏览器),它也不会起作用,因为这些cookie只会发送到您的服务器,而不是<target IP>
服务器。