无法从控制器方法中的表单获取值

时间:2014-03-11 19:01:35

标签: c# asp.net-mvc-4 asp.net-mvc-routing form-submit

我有一个MVC 4网站(.NET 4.5),如果他们试图访问网站的高分辨率照片(Hrp)部分,我想将用户重定向到一个非常简单的登录页面。我已经设置了路线,它们似乎工作正常,即用户被路由到'索引'页面或'登录'页。但是,当我在“登录”中收到数据时HrpController中的方法,即使我在表单中填写了用户名和密码字段,也是null。

应用程序的逻辑流程应如下所示:

  1. 用户进入索引页面(/ Hrp),如果用户未登录,他们将获得登录表单
  2. 用户填写登录表单并将其提交给HrpController.Login(/ Hrp / Login)
  3. HrpController.Login方法应该获取用户名和密码并根据硬编码值进行验证(我知道这是非常弱的安全性,但它是对现有代码的重写,我不能随意更改机制)
  4. 如果用户名和密码与表单上输入的内容匹配,则会向用户提供Hrp索引页面(/ Hrp)
  5. 如果用户名或密码不匹配,请将用户返回登录页面(/ Hrp /登录)
  6. 我不确定为什么用户名和密码值为null,而不是包含用户在表单上填写的值。任何帮助都非常感谢。

    路线:

            routes.MapRoute(
              name: "Hrp",
              url: "Hrp/",
              defaults: new { controller = "Hrp", action = "Index" }
            );
    
            routes.MapRoute(
                name: "HrpLogin",
                url: "Hrp/Login/",
                defaults: new { controller = "Hrp", action = "Login" }
            );
    
            routes.MapRoute(
                name: "HrpDetails",
                url: "Hrp/Details/{id}/",
                defaults: new { controller = "Hrp", action = "Details",
                                id = UrlParameter.Optional }
            );
    

    表格:

    @using MyApp.App_Code;
    
    @model MyApp.App_Code.Models.HrpUser
    
    
    @using (Html.BeginForm("Login", "Hrp", FormMethod.Post)) 
    {
        @Html.ValidationSummary(true, "Login failed. Check your login details.");
        <div>
             <div id="content" class="content">
                <section id="article">
                    <div class="cont orangeTopBorder">
                        <fieldset>
                            <legend>Login</legend>
                            <div class="editor-label">
                                @Html.LabelFor(u => u.username)
                            </div>
                            <div class="editor-field">
                                @Html.TextBoxFor(u => u.username)
                                @Html.ValidationMessageFor(u => u.username)
                            </div>
                            <div class="editor-label">
                                @Html.LabelFor(u => u.password)
                            </div>
                            <div class="editor-field">
                                @Html.PasswordFor(u => u.password)
                                @Html.ValidationMessageFor(u => u.password)
                            </div>
                            <input type="submit" value="Submit"/>
                        </fieldset>
                        <div class="footerarticle">
                        </div>
                    </div>
                </section>
            </div>
        </div>
    }
    

    控制器:

        public ActionResult Login(string username, string password)
        {
            if (username == "yourname" && password == "yourpassword")
            {
                SetHrpLogonAccepted();
    
                // Load Hrp object here, then show the main Hrp page
    
                return RedirectToAction("Index", hrp);
            }
            else
            {
                return View("Login");
            }
        }
    
        public ActionResult Index()
        {
            if (IsHrpLogonAccepted() == false)
            {
                return View("Login");
            }
            else
            {
                   // Load Hrp object here, then show the main Hrp page
    
                    return View("Index", hrp);
            }
       }
    

    HrpUser:

        public class HrpUser
        {
            [Required]
            [Display(Name = "User name")]
            public string username { get; set; }
    
            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string password { get; set; }
        }
    

    助手方法:

        private bool IsHrpLogonAccepted()
        {
            return Session["HrpLogonAccepted"] != null;
        }
    
        private void SetHrpLogonAccepted()
        {
            Session["HrpLogonAccepted"] = true;
        }
    

2 个答案:

答案 0 :(得分:0)

尝试将登录ActionResult更改为:

public ActionResult Login(HrpUser loginModel)
{
    if (loginModel.username == "yourname" && loginModel.password == "yourpassword")
    {
        SetHrpLogonAccepted();

        // Load Hrp object here, then show the main Hrp page

        return RedirectToAction("Index", hrp);
    }
    else
    {
        return View("Login");
    }
}

答案 1 :(得分:0)

表单已作为POST提交到Hrp / Login,浏览器正在执行301重定向到Hrp / Login /。在此重定向期间,它将数据作为GET而不是POST发送,因此它能够路由到我的HrpController.Login方法,因为它没有使用[HttpPost]属性进行修饰,默认情况下它是GET。

要解决这个问题,我现在正在使用[HttpPost]属性进行装饰。

  [HttpPost]
  public ActionResult Login(HrpUser loginModel)

我还在“登录/”中添加斜杠:

@using (Html.BeginForm("Login/", "Hrp", FormMethod.Post))

由于我不得不这样做,我认为我会看到做一个重定向,使我的表单位于/ Hrp / Login,并且我可以通过使用来摆脱黑客攻击:

@using (Html.BeginForm())
相关问题