2016-04-06 44 views
0

存在しない、次のエラーが私のコード を構築する際に必要な偽造防止のフォームフィールド「__RequestVerificationToken」が存在しない場合に発生しASP.NET MVC:必要な偽造防止のフォームフィールド「__RequestVerificationTokenは」

これは、コントローラ方法:

[HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel1 model) 
     { 
      if (ModelState.IsValid) 
      { 
       int month = DateTime.ParseExact(model.BirthMonth.ToString(), "MMMM", CultureInfo.CurrentCulture).Month; 
       DateTime birthDate = new DateTime(model.BirthYear, month, model.BirthDay); 
       var user = new ApplicationUser { Email = model.Email, UserName = model.UserName, Name = model.Name, Surname = model.Surname, BirthDate = birthDate, Country = model.Country, PhoneNumber = model.PhoneNumber }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        db.Users.Add(user); 

        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 
        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

        var users = db.Users.ToList(); 
        var adminUsers = db.Users.Where(u => u.IsAdmin == true).ToList(); 

        return RedirectToAction("Index", "Home"); 
       } 
       AddErrors(result); 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 

// VIEW

@model GAPT.Models.RegisterViewModel1 
@{ 
    ViewBag.Title = "Tours-Maltin"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<div id="content"> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-md-6"> 

        @Html.AntiForgeryToken() 
        <form action="@Url.Action("Register","Account")" method="post"> 
         @*"Index","Home"*@ 
         <div class="form-group" style="width:280px"> 
          @*<label for="name-login">Surname <p style="color:red; display:inline"> *</p></label> 
          <input type="text" class="form-control" id="name-login" required>*@ 
          @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.Name, new { @class = "form-control" }) 
          </div> 
         </div> 
         <div class="form-group" style="width:280px"> 
          @*<label for="surname-login">Surname <p style="color:red; display:inline"> *</p></label> 
          <input type="text" class="form-control" id="surname-register" required>*@ 
          @Html.LabelFor(m => m.Surname, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" }) 
          </div> 
         </div> 
         <div class="form-group"> 
          @*<label for="email-login">Email <p style="color:red; display:inline"> *</p></label> 
          <input type="email" class="form-control" id="email-login" required>*@ 
          @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
          </div> 
         </div> 
         <div class="form-group"> 
          <script> 
          </script> 
          <label for="dob-login" style="display:block;">Date of Birth <p style="color:red; display:inline"> *</p></label> 
          <select name="BirthDay" id="BirthDay" onchange="runDay()" style="width:91px;height:34px;font-size:14px; text-align:center" required> 
           <option value=""> - Day - </option> 
           <option value="1">1</option> 
           <option value="2">2</option> 
           <option value="3">3</option> 
           <option value="4">4</option> 
           <option value="5">5</option> 
           <option value="6">6</option> 
           <option value="7">7</option> 
           <option value="8">8</option> 
           <option value="9">9</option> 
           <option value="10">10</option> 
           <option value="11">11</option> 
          </select> 
          <select name="BirthMonth" id="BirthMonth" onchange="runMonth()" style="width:91px;height:34px;font-size:14px;text-align:center" required> 
           <option value=""> - Month - </option> 
           <option value="January">January</option> 
           <option value="Febuary">Febuary</option> 
           <option value="March">March</option> 
           <option value="April">April</option> 
           <option value="May">May</option> 
           <option value="June">June</option> 
           <option value="July">July</option> 
           <option value="August">August</option> 
          </select> 
          <select name="BirthYear" id="BirthYear" onchange="runYear()" style="width:91px; height:34px;font-size:14px; text-align:center" required> 
           <option value=""> - Year - </option> 
           <option value="2000">2000</option> 
           <option value="1999">1999</option> 
           <option value="1998">1998</option> 
           <option value="1997">1997</option> 
           <option value="1996">1996</option> 
           <option value="1995">1995</option> 
           <option value="1994">1994</option> 
           <option value="1993">1993</option> 
           <option value="1992">1992</option> 
           <option value="1991">1991</option> 
           <option value="1990">1990</option> 
           <option value="1989">1989</option> 
           <option value="1988">1988</option> 
           <option value="1987">1987</option> 
           <option value="1986">1986</option> 
           <option value="1985">1985</option> 
           <option value="1984">1984</option> 
           <option value="1983">1983</option> 
          </select> 

          <script> 
           function runDay() { 
            //if (document.getElementById("srt").value != "") { 
            var day = document.getElementById("Day").value; 
            document.getElementById("BirthDay").value = day; 
            //} 
           } 
          </script> 

          <script> 
           function runMonth() { 
            //if (document.getElementById("srt").value != "") { 
            var month = document.getElementById("BirthMonth").value; 
            document.getElementById("BirthMonth").value = month; 
            //} 
           } 
          </script> 
          @*@Html.DropDownListFor(m => m., month: month, htmlAttributes: new { @class = "BirthMonth" })*@ 

          <script> 
           function runYear() { 
            //if (document.getElementById("srt").value != "") { 
            var year = document.getElementById("Year").value; 
            document.getElementById("BirthYear").value = year; 
            //} 
           } 
          </script> 
         </div> 

         <div class="form-group"> 
          @*<label for="select-country" style="display:block;">Country <p style="color:red; display:inline"> *</p></label> 
          <input type="text" id="country" class="form-control" style="padding-right:25%;text-align:center;padding-left:25%;">*@ 
          @Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label", @id="country"}) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.Country, new { @class = "form-control" }) 
          </div> 

          <!--problem--> 
          <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>--> 

          <script src="~/Scripts/countrySelect.min.js"></script> 
          <script> 
           $("#country").countrySelect(); 
          </script> 
         </div> 

         <div class="form-group"> 
          @*<label for="username-login">Username<p style="color:red; display:inline"> *</p></label> 
          <input type="text" pattern='^(?=.{6,20}$)(?!.*[._-]{2})[a-z][a-z0-9._-]*[a-z0-9]$' title="Username needs to be between 6 to 20 characters and can only contain alphanumeric characters, _ . or - which cannot be used near each other" class="form-control" id="username-login" required>*@ 
          @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
          </div> 
         </div> 
         <div class="form-group"> 
          @*<label for="mobile-login" style="text-wrap:normal">Mobile Number - So we can send you important information and reminders about your tours <p style="color:red; display:inline"> *</p></label> 
          <input type="tel" class="form-control" id="phone" style="padding-right: 26.7%;" required>*@ 
          @Html.LabelFor(m => m.PhoneNumber, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control", id = "mobile" }) 
          </div> 
         </div> 
         <div class="form-group"> 
          @*<label for="password-login">Password <p style="color:red; display:inline"> *</p></label> 
          <input type="password" pattern='^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!#"$%()^&*_=+-]).{8,16}$' title='Password needs to be between 8 to 20 characters and must include alphanumeric characters. Special characters accepted _ - . which must not be near each other.' class="form-control" id="password-login" required>*@ 
          @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
          </div> 
         </div> 
         <div class="form-group"> 
          @*<label for="confirm-password-login">Confirm Password <p style="color:red; display:inline"> *</p></label> 
          <input type="password" class="form-control" id="confirm-password-login" required>*@ 
          @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
          <div class="col-md-10"> 
           @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
          </div> 
         </div> 


         <div class="text-center"> 
          <button type="submit" class="btn btn-template-main"><i class="fa fa-user-md"></i> Register</button> 
         </div> 
        </form> 
       </div> 
      </div> 
      <div class="col-md-6 hidden-sm hidden-xs"> 
       <div class="box" style="padding-left:14px; padding-right:14px;"> 
        <h2 class="text-uppercase">Login</h2> 

        <p class="lead">Already our customer?</p> 

        <hr> 

        <form action="@Url.Action("Login","Account")" method="post"> 
         <div class="form-group"> 
          <label for="text">Username or Email<p style="color:red; display:inline"> *</p></label> 
          <input type="text" class="form-control" id="email" required> 
         </div> 
         <div class="form-group"> 
          <label for="password">Password <p style="color:red; display:inline"> *</p></label> 
          <input type="password" class="form-control" id="password" required> 
          <br> 
          <a href="@Url.Action("PasswordReminder","Home")"<label style="text-decoration:underline">Forgot your Password?</label></a>       
         </div> 
         <div class="text-center" style="padding-top: 7px;"> 
          <button type="submit" class="btn btn-template-main"><i class="fa fa-sign-in"></i> Log in</button> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
     <!-- /.row --> 
    </div> 
    <!-- /.container --> 
</div> 
<!-- /#content --> 

は問題まだpersi、しかし他の質問を追いましたsts。

おかげ

答えて

0

はフォーム内のトークンを配置する必要があります:

<form action="@Url.Action("Register","Account")" method="post"> 
    @Html.AntiForgeryToken() 

form要素が開く前に、あなたがそれを置くしているので、それはフォームのPOSTには含まれていません。

+0

ありがとうございます!あなたは命の恩人です:) – Techworld

関連する問題