Advertisement
Guest User

Untitled

a guest
May 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  2. public class MyValidateAntiForgeryTokenAttribute : System.Web.Mvc.FilterAttribute, System.Web.Mvc.IAuthorizationFilter
  3. {
  4.  
  5. private void ValidateRequestHeader(HttpRequestBase request)
  6. {
  7. string cookieToken = String.Empty;
  8. string formToken = String.Empty;
  9. string tokenValue = request.Headers["RequestVerificationToken"];
  10. if (!String.IsNullOrEmpty(tokenValue))
  11. {
  12. string[] tokens = tokenValue.Split(':');
  13. if (tokens.Length == 2)
  14. {
  15. cookieToken = tokens[0].Trim();
  16. formToken = tokens[1].Trim();
  17. }
  18. }
  19. AntiForgery.Validate(cookieToken, formToken);
  20. }
  21. public void OnAuthorization(AuthorizationContext filterContext)
  22. {
  23.  
  24. try
  25. {
  26. if (filterContext.HttpContext.Request.IsAjaxRequest())
  27. {
  28. ValidateRequestHeader(filterContext.HttpContext.Request);
  29. }
  30. else
  31. {
  32. AntiForgery.Validate();
  33. }
  34. }
  35. catch (HttpAntiForgeryException e)
  36. {
  37. throw new HttpAntiForgeryException("Anti forgery token cookie not found");
  38. }
  39. }
  40. }
  41.  
  42. // POST: /Manage/ChangePassword
  43. [HttpPost]
  44. //[ValidateAntiForgeryToken]
  45. [MyValidateAntiForgeryToken]
  46. public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
  47. {
  48. if (!ModelState.IsValid)
  49. {
  50. return View(model);
  51. }
  52. var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
  53. if (result.Succeeded)
  54. {
  55. var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
  56. if (user != null)
  57. {
  58. await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
  59. }
  60. return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
  61. }
  62. AddErrors(result);
  63. return View(model);
  64. }
  65.  
  66. app.controller('MyCtrl', ['$scope', '$upload', '$http' , function ($scope, $upload, $http) {
  67.  
  68. $scope.sendpassword = function () {
  69. alert($scope.antiForgeryToken);
  70. $http({
  71. method: 'POST',
  72. url: '/Manage/ChangePassword',
  73. data: {
  74. OldPassword: $scope.OldPassword, NewPassword: $scope.NewPassword, ConfirmPassword: $scope.ConfirmPassword
  75. },
  76. headers: {
  77. 'RequestVerificationToken': $scope.antiForgeryToken
  78. }
  79. }).success(function (data) {
  80. alert(data);
  81. });
  82. }
  83.  
  84. <form data-ng-submit="sendpassword()" novalidate>
  85. <input id="antiForgeryToken" data-ng-model="antiForgeryToken" type="hidden" data-ng-init="antiForgeryToken='@GetAntiForgeryToken()'" />
  86. </form>
  87.  
  88. @functions{
  89. public string GetAntiForgeryToken()
  90. {
  91. string cookieToken, formToken;
  92. AntiForgery.GetTokens(null, out cookieToken, out formToken);
  93. return cookieToken + ":" + formToken;
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement