2013-10-08 6 views
1

내 컨트롤러가 오류ValidateAntiForgeryToken 던지는 오류

을 던지고 필요한 위조 방지 양식 필드 "__RequestVerificationToken"가 존재하지 않습니다.

그러나 이것은 내가 테스트 사용자에 로그인

  1. 을하고있는 중이 정확히 무엇

VIEW

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) 
{ 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary() 

     <fieldset> 
      <legend>Log in Form</legend> 
      <ol> 
       <li> 
        @Html.LabelFor(m => m.UserName) 
        @Html.TextBoxFor(m => m.UserName) 
       </li> 

       <li> 
        @Html.LabelFor(m => m.Password) 
        @Html.PasswordFor(m => m.Password) 
       </li> 

       <li> 
        @Html.LabelFor(m => m.RememberMe) 
        @Html.CheckBoxFor(m => m.RememberMe) 
       </li> 

CONTROLLER

,451,515,
[AllowAnonymous] 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Login(LoginModel model, string returnUrl) 
     { 
      if (ModelState.IsValid) 
      { 
       if (Membership.ValidateUser(model.UserName, model.Password)) 
       {      
        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 

        return RedirectToCreateUserProfile(model, returnUrl); 
       } 
       else 
       { 
        ModelState.AddModelError("", "The user name or password provided is incorrect."); 
       } 
  1. 내가

  2. 그때 내가 나에게 사용자 프로파일을 표시하려면 메뉴 옵션을 클릭 홈 페이지로 이동하고 있는데 위의 오류를

레이아웃보기를 얻을 한 번 인증 (더 많은 코드가 필요하지만 JS가 문제를 일으키고 싶어합니다.)

<!DOCTYPE html> 
<html> 
    <head> 
     <title>@ViewBag.Title</title> 
     <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
     <link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.common.min.css")" rel="stylesheet" type="text/css" /> 
     <link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.dataviz.min.css")" rel="stylesheet" type="text/css" /> 
     <link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.metro.min.css")" rel="stylesheet" type="text/css" /> 
     <link href="@Url.Content("~/Content/kendo/2013.2.918/kendo.dataviz.metro.min.css")" rel="stylesheet" type="text/css" /> 
     <script src="@Url.Content("~/Scripts/kendo/2013.2.918/jquery.min.js")"></script> 
     <script src="@Url.Content("~/Scripts/kendo/2013.2.918/kendo.all.min.js")"></script> 
     <script src="@Url.Content("~/Scripts/kendo/2013.2.918/kendo.aspnetmvc.min.js")"></script> 
     <script src="@Url.Content("~/Scripts/kendo.modernizr.custom.js")"></script> 
     <script type="text/javascript"> 

      var _gaq = _gaq || []; 
      var pluginUrl = 
      '//www.google-analytics.com/plugins/ga/inpage_linkid.js'; 
      _gaq.push(['_require', 'inpage_linkid', pluginUrl]); 
      _gaq.push(['_setAccount', 'UA-44529127-1']); 
      _gaq.push(['_trackPageview']); 

      (function() { 
       var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
       ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
      })(); 

</script> 

    </head> 
    <body> 
     <header> 
      <div class="content-wrapper"> 
       <div class="float-left"> 
        <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p> 
       </div> 
       <div class="float-right"> 
        <section id="login"> 
         @Html.Partial("_LoginPartial") 
        </section> 
        <nav> 
         <ul id="menu"> 
          <li>@Html.ActionLink("Home", "Index", "Home")</li> 
          <li>@Html.ActionLink("About", "About", "Home")</li> 
          <li>@Html.ActionLink("Contact", "Contact", "Home")</li> 
          @if (User.IsInRole("Admin")) 
          { 
           <li>@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)</li>        
          } 
         </ul> 
        </nav>      
       </div> 
      </div> 
     </header> 
     <div id="body"> 
      @if (Request.IsAuthenticated) 
      { 
      <ul id="IndexHomeMenu"> 
       @if (User.IsInRole("Admin")) 
       { 
        <li> 
         [email protected]*@Html.ActionLink("Administration", "Contact", "Home")*@ 
         <ul> 
          <li>@Html.ActionLink("Manage Roles", "Index", "AdminView")</li> 
          <li>@Html.ActionLink("Manage Users", "Contact", "Home")</li> 
          <li>@Html.ActionLink("Inactive Reasons", "Index", "InactiveReasonView")</li> 
         </ul> 
        </li> 
       } 
       <li> 
        My Information 
        <ul> 
         <li>@Html.ActionLink("Profile", "EditByName", "UserView", new { UserName = User.Identity.Name }, new { @class = "selected" })</li> 
         <li>@Html.ActionLink("Phone Numbers", "Active", "PhoneNumberView",new {userName= User.Identity.Name },null)</li> 
         <li>@Html.ActionLink("Address's", "Active", "AddressView",new {userName= User.Identity.Name },null)</li> 
         @if(!User.IsInRole("Clients")){ 
         <li>@Html.ActionLink("Subscription", "Index", "AdminView")</li>} 
        </ul> 
내가
  • @ Html.ActionLink 클릭하고 53,691,363,210

    ("프로필", "EditByName", "UserView"새 {사용자 이름 = User.Identity.Name}, 새 {@class는 = "선택"})

  • CONTROLLER

    [ValidateAntiForgeryToken] 
        public ActionResult EditByName(string userName)//EditByName 
        { 
         if (User.Identity.IsAuthenticated) 
         { 
          UserModel usermodel = repository.Get(User.Identity.Name);// db.UserModels.Find(id); 
          if (usermodel == null) 
          { 
           return RedirectToAction("Create","UserView", User.Identity.Name); 
          } 
          return View(usermodel); 
         } 
         else { return RedirectToAction("Login", controllerName: "AccountView"); } 
        } 
    

    오류가 발생하는 경우가있다. 나는 무엇을 놓치고 있는지 확실하지 않고, 토큰을 만들고 있으며, 모든 형태로되어 있습니다.

    답변

    2

    GET 동작 (EditByName 동작)에서 [ValidateAntiForgeryToken]을 사용하고 있지만 POST 동작에서 작동합니다.

    [ValidateAntiForgeryToken]의 경우 question을 참조하고 CSRF 공격을 방지하는 방법을 설명하는 article을 참조하십시오.

    1

    EditByNameGET -action 메소드에서 [ValidateAntiForgeryToken]을 제거하십시오.

    또한 if (User.Identity.IsAuthenticated) 대신 [Authorize] 속성을 사용하십시오.

    모든 사용자가 사용자 이름을 알고있는 한 프로필을 편집 할 수 있습니까?

    +0

    ADMIN 역할의 사용자 만 모든 프로필을 편집/볼 수 있습니다. – ChampChris

    +0

    그리고 컨트롤러 상단에 [인증] 권한이 있습니다. – ChampChris