2016-03-24 4 views
0

Ich kann das RequestVerificationToken nicht von der Webseite an den Server mit AngularJs übergeben.Das erforderliche fälschungssichere Formularfeld "__RequestVerificationToken" ist nicht vorhanden. AngularJs MVC

Mein AngularJS-Code ist:

var app = angular.module('validation', []); 
app.controller('SignUpController', function ($scope, $http) { 
    $scope.model = {}; 
    $scope.email = {}; 
    $scope.sendEmail = function() { 
     $http({ 
      method: 'POST', 
      url: '/Contact/Test', 
      data: $scope.email, 
      headers: { 
       'RequestVerificationToken': $scope.antiForgeryToken 
      } 
     }).success(); 
    }; 
}); 

Custom Code Attribut:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
    public class CustomAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter 
    { 


     private void ValidateRequestHeader(HttpRequestBase request) 
     { 
      string cookieToken = String.Empty; 
      string formToken = String.Empty; 
      string tokenValue = request.Headers["RequestVerificationToken"]; 
      if (!String.IsNullOrEmpty(tokenValue)) 
      { 
       string[] tokens = tokenValue.Split(':'); 
       if (tokens.Length == 2) 
       { 
        cookieToken = tokens[0].Trim(); 
        formToken = tokens[1].Trim(); 
       } 
      } 
      AntiForgery.Validate(cookieToken, formToken); 
     } 

     public void OnAuthorization(AuthorizationContext filterContext) 
     { 

      try 
      { 
       if (filterContext.HttpContext.Request.IsAjaxRequest()) 
       { 
        ValidateRequestHeader(filterContext.HttpContext.Request); 
       } 
       else 
       { 
        AntiForgery.Validate(); 
       } 
      } 
      catch (HttpAntiForgeryException e) 
      { 
       throw new HttpAntiForgeryException("Anti forgery token cookie not found"); 
      } 
     } 
    } 

Form ist:

@functions{ 
    public string GetAntiForgeryToken() 
    { 
     string cookieToken, formToken; 
     AntiForgery.GetTokens(null, out cookieToken, out formToken); 
     return cookieToken + ":" + formToken; 
    } 
} 
<div ng-app="validation" ng-controller="SignUpController"> 
    <form role="form" id="frmContact" action="@Url.Action("Index", "Contact")" method="POST"> 
     <input id="antiForgeryToken" ng-model="antiForgeryToken" type="hidden" ng-init="antiForgeryToken='@GetAntiForgeryToken()'" /> 
     <fieldset class="form-group"> 
      @Html.LabelFor(x => x.EmailTitle) 
      @Html.TextBoxFor(x => x.EmailTitle, new { placeholder = @Resource.EmailTitle, @class = "form-control", data_ng_model = "new.email.title" }) 
     </fieldset> 
     <fieldset class="form-group"> 
      @Html.LabelFor(x => x.EmailAddress) 
      @Html.TextBoxFor(x => x.EmailAddress, new { placeholder = @Resource.EmailAddress, @class = "form-control", data_ng_model = "new.email.address" }) 
     </fieldset> 
     <fieldset class="form-group"> 
      @Html.LabelFor(x => x.EmailMessage) 
      @Html.TextAreaFor(x => x.EmailMessage, new { placeholder = @Resource.EmailMessage, @class = "form-control", data_ng_model = "new.email.message" }) 
     </fieldset> 


     <div> 
      <button type="submit" name="btnEmailForm" id="btnEmailForm" class="btnLogin" ng-click="sendEmail()" value="sendMessage">@Resource.ContactFormSendMessageButton</button> 
     </div> 
     <div id="errorMessages" class="error">{{message}}</div> 
    </form> 
</div> 

ich folgende Beiträge gelesen haben, kann aber nicht zu lösen scheinen die Problem, und nahm auch Code von https://github.com/techbrij/angularjs-asp-net-mvc, die in diesem Beispiel funktioniert, aber nicht in meiner MVC-Anwendung ion:

http://techbrij.com/angularjs-antiforgerytoken-asp-net-mvc

https://parthivpandya.wordpress.com/2013/11/25/angularjs-and-antiforgerytoken-in-asp-net-mvc/

AngularJS Web Api AntiForgeryToken CSRF

http://bartwullems.blogspot.co.uk/2014/10/angularjs-and-aspnet-mvc-isajaxrequest.html

Where exactly to put the antiforgeryToken

http://www.ojdevelops.com/2016/01/using-antiforgerytokens-in-aspnet-mvc.html

Kann jemand mit diesem Problem

+0

Nicht klar: auf btnEmailForm klicken Sie möchten, dass Sie Ihr Formular an Index/Kontakt senden und gleichzeitig Post-Anfrage an/Kontakt/Test durchführen? Und Ihr benutzerdefiniertes Attribut: 'CustomAntiForgeryTokenAttribute', für welche dieser Aktionen es angewendet wurde? Und auch die antiForgeryToken-Eingabe hat kein Attribut 'name = '__RequestVerificationToken', deshalb geht es nicht zum Server. –

+0

Mein Fehler, frustriert, da dieses Problem nicht gelöst werden kann. Index/Kontakt sollte/Kontakt/Test sein –

Antwort

0

In diesem Fall hilft Ihnen Form submit und $scope.sendEmail Operationen durchführen und sie können miteinander in Konflikt stehen, um dieses Verhalten zu verhindern, dass Sie ng-submit Richtlinie verwenden können. Fügen Sie auch Attribute hinzu: name= '__RequestVerificationToken' und ng-value="antiForgeryToken" zu entsprechenden input.

+0

Ich denke, ich werde versuchen, es auszuarbeiten –

Verwandte Themen