正确地模拟MVC ASP.NET中的HTML动作

本文关键字:中的 HTML 动作 NET ASP 模拟 MVC 正确地 | 更新日期: 2023-09-27 18:16:35

我需要在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"answers"password",并将它们发送到一个URI。我在我的应用程序中尝试了"GET"answers"POST"方法,但它不起作用。这是我的控制器在MVC ASP。净:

    [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 inspect元素发送状态码200的请求。这是一个网页,一个网络控制器重定向所有的流量到它,然后用户必须输入用户名和密码。然后,当它通过POST或GET发送时,网络控制器必须捕获参数并验证它们。在验证成功的情况下,网络控制器将用户重定向到他最初请求的页面,以便他可以浏览web。这个简单的HTML页面实现了所有这些,但在MVC应用程序中,我一次又一次地被重定向到相同的登录页面。你知道有什么问题吗?

正确地模拟MVC ASP.NET中的HTML动作

这不起作用,因为身份验证cookie没有存储在浏览器中。相反,它被交给您的服务器(MVC应用程序)。

用你目前的方法是无法达到你的目标的。因为即使您的服务器将这些cookie发送给客户端(浏览器),它也不会工作,因为这些cookie只会发送到您的服务器,而不是<target IP>服务器。