使用ValidateAntiForgeryToken ASP.NET MVC 3时无法验证数据错误

时间:2014-03-10 13:43:44

标签: c# asp.net asp.net-mvc asp.net-mvc-3 iis-7.5

我有注册表格,我有商业关键字第一个搜索关键字然后它显示在列表框然后我选择商业关键字,并在另一个列表框中选择

This is the form of business keywords

当我发布此表单并重定向到下一个表单时,我收到错误无法验证数据

This is error message i have return

我也在我的web.config文件中使用机器密钥这个问题错误在我的网站迁移到另一台服务器时我也改变了托管服务提供商然后我面临同样的问题。我在IIS 7.5中托管我的网站,所以我需要web.config文件中的任何设置。 Ë 如果我在web.config文件中使用机器密钥标签,那么Model返回null值,因此它给出错误消息对象引用null我在其他免费服务器中托管我的网站然后它的工作非常文件没有任何错误消息

我离线工作的网站非常好,没有任何错误信息,任何人都可以向我推荐解决方案

我正在使用带有c#.net razor视图引擎的Asp.net MVC 3。我推荐很多网站,但我没有得到任何解决方案。据我所知,在IIS 7.5的web.config文件中做了一些设置,但是

我不知道我做了哪些设置?任何人都可以在线测试本网站访问:contactsdetail.com并点击FreeListing标签,然后继续填写此表单,然后出现此错误

如果有任何人对此错误有任何疑问,请通知我。

提前致谢

我首先在代码下方免费列出了查看页面:

LocationInformation.chtml

@{using (Html.BeginForm("LocationInformation", "FreeListing", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      @Html.AntiForgeryToken("PostData");
    <div>
        <fieldset>
            <legend>Local Information</legend>
            @Html.HiddenFor(model => model.ContactsID)
            <br />
            <div class="divlabel">
                <div id="divlabeltype">
                    Business Name <span class="spanid">*</span></div>
                @Html.TextBoxFor(model => model.CompanyName)
                @Html.ValidationMessageFor(m => m.CompanyName, string.Empty, new { @class = "validationfont" })
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    Building</div>
                @Html.TextBoxFor(model => model.Building)
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    Street</div>
                @Html.TextBoxFor(model => model.Street)
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    LandMark</div>
                @Html.TextBoxFor(model => model.LandMark)
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    Area<span class="spanid">*</span></div>
                    @Html.TextBoxFor(m => m.AreaName, new { @id = "txtsearcharea" }) 
           @*     @Html.TextBox("AreaName", "", new { @id = "txtsearcharea" })*@
                @Html.ValidationMessageFor(m => m.AreaName,null,new { @class = "validationfont" })
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    City <span class="spanid">*</span></div>
                    @Html.TextBoxFor(m=>m.CityName,new{@id = "txtsearchkey1"})
              @*  @Html.TextBox("CityName",, new { @id = "txtsearchkey1", @readonly = "readonly" })*@
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    Pincode</div>
                @Html.TextBoxFor(model => model.Pincode)
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    State</div>
                @Html.TextBox("StateName", (string)ViewData["statenm"], new { @id = "txtsearchkey2", @readonly = "readonly" })
            </div>
            <div class="divlabel">
                <div id="divlabeltype">
                    Country</div>
                @Html.TextBox("CountryName", (string)ViewData["countrynm"], new { @id = "txtsearchkey3", @readonly = "readonly" })
            </div>
            <div>
                @{
      int newpageno = 1;
      if (Request.Params["page"] != null)
      {
          newpageno = int.Parse(Request.Params["page"].ToString());
          newpageno = newpageno + 1;
      }
       <div class="buttonsavecontinue">
         <div class="submitbutton"> 

                        <a href="#"><input type="submit" name="SaveContinue" value="Save & Continue" /></a> 
                         <a href="#">
                            <input type="submit" name="SaveExit" value="Save & Exit" /></a>
                        </div>
                    </div>


                     <div class="buttonrightalign">
                       <div class="submitbutton"> 
                          @Html.ActionLink("Next >>", "fl_Index", new { id = 2 })
                     @*  <a href="@Url.Content("~/FreeListing/fl_Index/2")"><input type="submit" value="Next >>" /> </a>*@ </div>
                    </div>
                }
            </div>
        </fieldset>
    </div>
  }
}

FreeListingController

        [HttpPost]
        [ValidateAntiForgeryToken(Salt = "PostData")]
        public ActionResult LocationInformation(string SaveExit, string SaveContinue, FreeListingTable model, HttpPostedFileBase uploadFile)
        {
            db = new DirectoryDBEntities();
            if (Session["editcontactsid"] != null)
            {
                editcontactsid = Convert.ToInt32(Session["editcontactsid"]);
                contactids = editcontactsid;
            }
            if (Session["contactids"] != null)
            {
                contactids = Convert.ToInt32(Session["contactids"]);
            }
            if (Session["isnewuser"] != null)
            {
                isnewuser = Convert.ToBoolean(Session["isnewuser"]);
            }
            if (Session["fltable"] != null)
            {
                fltable = ((FreeListingTable)Session["fltable"]);

            }
            if (model.CompanyName == null)
            {
                ModelState.AddModelError("CompanyName", "Please Enter Business Name");
                fltable.CompanyName = null;
                return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
            }


            if (model.AreaName == null)
            {
                ModelState.AddModelError("AreaName", "Please Enter AreaName");
                return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);

            }


            if (SaveExit == "Save & Exit")
            {


                if (model.CompanyName == null)
                {
                    ModelState.AddModelError("CompanyName", "Please Enter Business Name");
                    fltable.CompanyName = null;
                    return RedirectToAction("fl_Index", "FreeListing", new { id = 1 });
                }

                if (model.AreaName == null)
                {
                    ModelState.AddModelError("AreaName", "Please Enter AreaName");
                    return RedirectToAction("fl_Index", "FreeListing", new { id = 1 });
                }


                if (contactids == 0)
                {
                    model.CityID = fltable.CityID;
                    model.CompanyName = fltable.CompanyName;
                    model.CityID = fltable.CityID;
                    model.ContactName = fltable.ContactName;
                    model.Designation = fltable.Designation;
                    model.PhoneNo = fltable.PhoneNo;
                    model.MobileNo = fltable.MobileNo;
                    model.MoblieNo2 = fltable.MoblieNo2;
                    if (fltable.LandMark != null && fltable.LandMark != string.Empty)
                        model.LandMark = fltable.LandMark;
                    else
                        fltable.LandMark = model.LandMark;
                    model.EmailID = fltable.EmailID;
                    model.Website = fltable.Website;
                    model.Pincode = fltable.Pincode;
                    var getareaid = db.AreaTables.Where(t => t.AreaName == model.AreaName).SingleOrDefault();
                    if (getareaid != null)
                    {
                        model.AreaID = getareaid.AreaID;
                    }
                    if (model.AreaID == null)
                    {
                        fltable.AreaName = model.AreaName;
                    }
                    else
                    {
                        fltable.AreaID = model.AreaID;
                        ViewData["AreaID"] = model.AreaID;
                    }
                    model.IsPremium = false;
                    var getcitydata = db.CityTables.Where(t => t.CityID == model.CityID).SingleOrDefault();
                    if (getcitydata != null)
                    {
                        model.CityName = getcitydata.CityName;
                        model.CountryName = getcitydata.CountryTable.CountryName;
                        model.StateName = getcitydata.StateTable.StateName;
                        model.StateID = getcitydata.StateID;
                        model.CountryID = getcitydata.CountryID;
                        TempData["CityName"] = model.CityName;
                    }

                    model.Date = DateTime.Today.Date;

                    db.AddToFreeListingTables(model);
                    db.SaveChanges();
                    contactids = model.ContactsID;
                    Session["fltable"] = fltable;


                    Session["contactids"] = contactids;
                }
                else
                {
                    FreeListingTable getcurrentdata = db.FreeListingTables.Where(t => t.ContactsID == contactids).SingleOrDefault();
                    if (getcurrentdata != null)
                    {
                        getcurrentdata.CompanyName = model.CompanyName;
                        getcurrentdata.Building = model.Building;
                        getcurrentdata.Street = model.Street;
                        getcurrentdata.LandMark = model.LandMark;
                        getcurrentdata.Pincode = model.Pincode;

                        var getareaid = db.AreaTables.Where(t => t.AreaName == model.AreaName).SingleOrDefault();
                        if (getareaid != null)
                        {
                            getcurrentdata.AreaID = getareaid.AreaID;
                            model.AreaID = getareaid.AreaID;

                        }
                        if (model.AreaID == null)
                        {

                            fltable.AreaName = model.AreaName;
                            model.AreaID = null;
                            fltable.AreaID = null;
                            getcurrentdata.AreaID = null;
                            getcurrentdata.AreaName = model.AreaName;

                            db.SaveChanges();
                        }
                        else
                        {
                            fltable.AreaID = model.AreaID;
                            ViewData["AreaID"] = model.AreaID;
                        }

                        if (model.CityName != null && model.CityName != string.Empty)
                        {
                            var getcityid = db.CityTables.Where(t => t.CityName == model.CityName).SingleOrDefault();
                            if (getcityid != null)
                            {
                                fltable.CityID = getcityid.CityID;
                            }
                        }

                        var getcitydata = db.CityTables.Where(t => t.CityID == fltable.CityID).SingleOrDefault();
                        if (getcitydata != null)
                        {
                            getcurrentdata.CityName = getcitydata.CityName;
                            getcurrentdata.CountryName = getcitydata.CountryTable.CountryName;
                            getcurrentdata.StateName = getcitydata.StateTable.StateName;
                            getcurrentdata.StateID = getcitydata.StateID;
                            getcurrentdata.CountryID = getcitydata.CountryID;
                            TempData["CityName"] = getcurrentdata.CityName;
                            model.IsPremium = false;
                        }
                        getcurrentdata.Date = DateTime.Today.Date;
                        db.SaveChanges();
                        fltable = getcurrentdata;
                        model = getcurrentdata;

                    }
                }

                contactids = model.ContactsID;
                Session["fltable"] = fltable;
                model = fltable;

                if (model.ContactName == null)
                {
                    ModelState.AddModelError("ContactName", "Please Enter Contact Name");
                    fltable.ContactName = null;

                    return RedirectToAction("fl_Index", "FreeListing", new { id = 2 });
                }
                if (model.PhoneNo == null && model.PhoneNo == string.Empty && model.MobileNo == null && model.MobileNo == string.Empty)
                {

                    ModelState.AddModelError("MobileNo", "Please Enter Mobile No 1");
                    fltable.MobileNo = null;
                    return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
                }
                if (isnewuser)
                {

                    if (model.OldPassword == null || model.OldPassword == string.Empty)
                    {
                        ModelState.AddModelError("keyName", "Please enter old password");
                        return RedirectToAction("fl_Index", "FreeListing", new { id = 6 });

                    }
                    if (model.NewPassword == null || model.NewPassword == string.Empty)
                    {
                        ModelState.AddModelError("newpassword", "Please enter new password");
                       return RedirectToAction("fl_Index", "FreeListing", new { id = 6 });

                    }
                    if (model.NewPassword != null || model.NewPassword != string.Empty)
                    {
                        if (model.NewPassword != model.ConfirmPassword)
                        {
                            ModelState.AddModelError("ConfirmPassword", "Password not match with new password");
                            return RedirectToAction("fl_Index", "FreeListing", new { id = 6 });
                        }
                    }
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }

            }
            if (SaveContinue == "Save & Continue")
            {

                if (contactids == 0)
                {
                    model.CompanyName = fltable.CompanyName;
                    model.CityID = fltable.CityID;
                    model.ContactName = fltable.ContactName;
                    model.Designation = fltable.Designation;
                    model.PhoneNo = fltable.PhoneNo;
                    model.MobileNo = fltable.MobileNo;
                    model.MoblieNo2 = fltable.MoblieNo2;
                    model.EmailID = fltable.EmailID;
                    model.Website = fltable.Website;
                    if (fltable.LandMark != null && fltable.LandMark != string.Empty)
                        model.LandMark = fltable.LandMark;
                    else
                        fltable.LandMark = model.LandMark;
                    model.Pincode = fltable.Pincode;
                    var getareaid = db.AreaTables.Where(t => t.AreaName == model.AreaName).SingleOrDefault();
                    if (getareaid != null)
                    {
                        model.AreaID = getareaid.AreaID;
                    }
                    if (model.AreaID == null)
                    {
                        fltable.AreaName = model.AreaName;
                    }
                    else
                    {
                        fltable.AreaID = model.AreaID;
                        ViewData["AreaID"] = model.AreaID;
                    }
                    model.IsPremium = false;

                    var getcitydata = db.CityTables.Where(t => t.CityID == model.CityID).SingleOrDefault();
                    if (getcitydata != null)
                    {
                        model.CityName = getcitydata.CityName;
                        model.CountryName = getcitydata.CountryTable.CountryName;
                        model.StateName = getcitydata.StateTable.StateName;
                        model.StateID = getcitydata.StateID;
                        model.CountryID = getcitydata.CountryID;
                        TempData["CityName"] = model.CityName;
                    }

                    model.Date = DateTime.Today.Date;
                    db.AddToFreeListingTables(model);
                    db.SaveChanges();
                    contactids = model.ContactsID;
                    Session["fltable"] = fltable;
                    Session["contactids"] = contactids;
                }
                else
                {
                    FreeListingTable getcurrentdata = db.FreeListingTables.Where(t => t.ContactsID == contactids).SingleOrDefault();
                    if (getcurrentdata != null)
                    {
                        getcurrentdata.CompanyName = model.CompanyName;
                        getcurrentdata.Building = model.Building;
                        getcurrentdata.Street = model.Street;
                        getcurrentdata.LandMark = model.LandMark;
                        getcurrentdata.Pincode = model.Pincode;
                        //Contact information
                        getcurrentdata.ContactName = fltable.ContactName;
                        getcurrentdata.Designation = fltable.Designation;
                        getcurrentdata.PhoneNo = fltable.PhoneNo;
                        getcurrentdata.MobileNo = fltable.MobileNo;
                        getcurrentdata.MoblieNo2 = fltable.MoblieNo2;
                        getcurrentdata.EmailID = fltable.EmailID;
                        getcurrentdata.Website = fltable.Website;

                        var getareaid = db.AreaTables.Where(t => t.AreaName == model.AreaName).SingleOrDefault();
                        if (getareaid != null)
                        {
                            getcurrentdata.AreaID = getareaid.AreaID;
                            getcurrentdata.AreaName = getareaid.AreaName;
                            fltable.AreaID = getareaid.AreaID;
                            fltable.AreaName = getareaid.AreaName;
                        }
                        if (model.AreaID == null)
                        {

                            fltable.AreaName = model.AreaName;
                            model.AreaID = null;
                            fltable.AreaID = null;
                            getcurrentdata.AreaID = null;
                            getcurrentdata.AreaName = model.AreaName;

                            db.SaveChanges();
                        }
                        else
                        {
                            fltable.AreaID = model.AreaID;
                            ViewData["AreaID"] = model.AreaID;
                        }


                        if (model.CityName != null && model.CityName != string.Empty)
                        {
                            var getcityid = db.CityTables.Where(t => t.CityName == model.CityName).SingleOrDefault();
                            if (getcityid != null)
                            {
                                fltable.CityID = getcityid.CityID;
                            }
                        }


                        var getcitydata = db.CityTables.Where(t => t.CityID == fltable.CityID).SingleOrDefault();
                        if (getcitydata != null)
                        {
                            getcurrentdata.CityName = getcitydata.CityName;
                            getcurrentdata.CountryName = getcitydata.CountryTable.CountryName;
                            getcurrentdata.StateName = getcitydata.StateTable.StateName;
                            getcurrentdata.StateID = getcitydata.StateID;
                            getcurrentdata.CountryID = getcitydata.CountryID;
                            getcurrentdata.CityID = getcitydata.CityID;
                            TempData["CityName"] = getcurrentdata.CityName;
                        }
                        getcurrentdata.Date = DateTime.Today.Date;
                        db.SaveChanges();
                        model = getcurrentdata;

                    }
                }

                contactids = model.ContactsID;
                Session["fltable"] = fltable;
                Session["contactids"] = contactids;

                return RedirectToAction("fl_Index", "FreeListing", new { id = 2 });
            }


            return RedirectToAction("fl_Index", "FreeListing", new { id = 2 });
        }
fl_index / 1中的

表示请求位置信息的视图 与fl_index / 2相同意味着请求查看联系信息 与fl_index / 3相同意味着请求在问题顶部发布屏幕截图的业务关键字视图

所以,当我点击保存并继续并请求下一页天气时,它是天气联系信息,它是LocationInformation或BusinessKeywords

1 个答案:

答案 0 :(得分:1)

您的表单不包含@Html.AntiForgeryToken()

我没有在你的动作方法中看到ValidateAntiForgeryTokenAttribute,所以我猜它是在控制器级别声明的...也许它也影响了GET请求?仅在POST方法中使用该属性。

首先尝试直接做事:http://blog.stevensanderson.com/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/