Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:05:34+00:00 2026-06-11T01:05:34+00:00

I have been facing serious problem with @Html.AntiForgeryToken() . I have a register controller

  • 0

I have been facing serious problem with @Html.AntiForgeryToken() . I have a register controller which had a create view to create/register new members. For that reason I used a @Html.AntiForgeryToken() without using any SALT in my main submit form. Now I would like to validate user name if it is already exist on the database on the blur event of my user name textbox. For this validation I wrote a new controller named ‘Validation’ and wrote a method with a constant validation SALT:

 [HttpPost]
    [ValidateAntiForgeryToken(Salt = @ApplicationEnvironment.SALT)]
    public ActionResult username(string log) {
        try {
            if (log == null || log.Length < 3)
                return Json(log, JsonRequestBehavior.AllowGet);

            var member = Membership.GetUser(log);

            if (member == null) {
                //string userPattern = @"^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$";
                string userPattern = "[A-Za-z][A-Za-z0-9._]{3,80}";
                if (Regex.IsMatch(log, userPattern))
                    return Json(log, JsonRequestBehavior.AllowGet);
            }

        } catch (Exception ex) {
            CustomErrorHandling.HandleErrorByEmail(ex, "Validate LogName()");
            return Json(log, JsonRequestBehavior.AllowGet);
        }
        //found e false
        return Json(log, JsonRequestBehavior.AllowGet);

    }

Method is working fine . I had checked with the HTTP Get annotation without the [ValidateAntiForgeryToken] and it giving me the expected results.

I had googled and checked many of the given solutions none of these are working. For my validation controller I used another form in the same page and used a SALT in the Anti-forgery token.

Example:
First anti-forgery token for the main submit form:

@using (Html.BeginForm(“Create”, “Register”)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true) … }

Second anti-forgery token:

<form id="__AjaxAntiForgeryForm" action="#" method="post">
    @Html.AntiForgeryToken(SALT)
</form> 

and in the javascript I used this

<script type="text/javascript" defer="defer">
    $(function () {
        AddAntiForgeryToken = function (data) {
            data.__RequestVerificationToken = $('#__AjaxAntiForgeryForm input[name=__RequestVerificationToken]').val();
            return data;
        };

        if ($("#LogName").length > 0) {

            $("#LogName").blur(function () {
                var user = $("#LogName").val();
                var logValidate = "/Validation/username/";
                //var URL = logValidate + user;
                //var token = $('#validation input[name=__RequestVerificationToken]').val();
                data = AddAntiForgeryToken({ log: user });

                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: logValidate,
                    data: data,
                    success: function (response) {
                        alert(response);
                    }
                });

            });

        }
    });
</script>

In my firebug I got this :

log=admin&__RequestVerificationToken=NO8Kds6B2e8bexBjesKlwkSexamsruZc4HeTnFOlYL4Iu6ia%2FyH7qBJcgHusekA50D7TVvYj%2FqB4eZp4VDFlfA6GN5gRz7PB%2ByZ0AxtxW4nT0E%2FvmYwn7Fvo4GzS2ZAhsGLyQC098dfIJaWCSiPcc%2FfD00FqKxjvnqmxhXvnEx2Ye83LbfqA%2F4XTBX8getBeodwUQNkcNi6ZtAJQZ79ySg%3D%3D

as passed but in the cookie section I got a different cookie than passing one:
Actual Cookie:

ws5Dt2if6Hsah rW2nDly P3cW1smIdp1Vau 0TXOK1w0ctr0BCso/nbYu w9blq/QcrXxQLDLAlKBC3Tyhp5ECtK MxF4hhPpzoeByjROUG0NDJfCAlqVVwV5W6lw9ZFp/VBcQmwBCzBM/36UTBWmWn6pMM2bqnyoqXOK4aUZ4=

I think this is because I used 2 anti-forgery tokens in one page. But in my mind I should use 2 because first one is generating for the submit to occur and next one is need to verify the validation. However, this is my guess and I think I am wrong and for this reason I need help from you guys.

Can anyone please explain the facts that should I use two anti-forgery or one?

Thank you all in advance….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:05:36+00:00Added an answer on June 11, 2026 at 1:05 am

    Finally 8 hours of struggling gives me a solution.

    First things first, yes there is no harm to use 2 anti-forgery tokens in the page. And second there is no need to match the cookies with the providing token. Providing token will be always different and will be verified in the server.

    Anti-forgery token does not work if we use [HttpGet] action verbs.. Because in the validation process of anti-forgery the token is validated by retrieving of the Request.Form['__RequestVerificationToken'] value from the request. ref : Steven Sanderson’s blog: prevent cross…

    Solution :

    My modifies controller:

    [HttpPost]
    
            [ValidateAntiForgeryToken(Salt = @ApplicationEnvironment.SALT)]
    //change the map route values to accept this parameters.
            public ActionResult username(string id, string __RequestVerificationToken) {
            string returnParam = __RequestVerificationToken;
    
            try {
                if (id == null || id.Length < 3)
                    return Json(returnParam, JsonRequestBehavior.AllowGet);
    
                var member = Membership.GetUser(id);
    
                if (member == null) {
                    //string userPattern = @"^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$";
                    string userPattern = "[A-Za-z][A-Za-z0-9._]{3,80}";
                    if (Regex.IsMatch(id, userPattern))
                        return Json(returnParam, JsonRequestBehavior.AllowGet);
                }
    
            } catch (Exception ex) {
                CustomErrorHandling.HandleErrorByEmail(ex, "Validate LogName()");
                return Json(returnParam, JsonRequestBehavior.AllowGet);
            }
            //found e false
            return Json(returnParam, JsonRequestBehavior.AllowGet);
        }
    

    My first Form in the same page:

    @using (Html.BeginForm("Create", "Register")) {
    
        @Html.AntiForgeryToken(ApplicationEnvironment.SALT)
    
        @Html.ValidationSummary(true)
        ....
    }
    

    My second Form in the same page:

    **<form id="validation">
        <!-- there is harm in using 2 anti-forgery tokens in one page-->
        @Html.AntiForgeryToken(ApplicationEnvironment.SALT)
        <input type="hidden" name="id" id="id" value="" />
    </form>**
    

    My jQuery to solve this thing:

     $("#LogName").blur(function () {
                var user = $("#LogName").val();
                var logValidate = "/Validation/username/";
                $("#validation #id").val(user);
                **var form = $("#validation").serialize(); // a form is very important to verify anti-forgery token and data must be send in a form.**
    
                var token = $('input[name=__RequestVerificationToken]').val();
    
                $.ajax({
                    **type: "POST", //method must be POST to validate anti-forgery token or else it won't work.**
                    dataType: "JSON",
                    url: logValidate,
                    data: form,
                    success: function (response) {
                        alert(response);
                    }
                });
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tinkering around with PDF::API2 and i am facing a problem, create
I have been facing difficult time finding which browsers support which HTML 5 API
I have been facing some serious issues while executing this particular JSP page. CODE
I have been facing a strange problem while returning dataset through WCF service (WSHttpBinding).
I have been facing so many problem using the anonymous class like I can't
I have been integrating my website with oscommerce..am facing the problem with session..if I
i have been facing serious trouble in making work cucumber-js in windows. i start
Since some days, I have been facing a problem in Visual Studio 2008, related
I have a problem about weblogic console. I have been facing with this problem
Been Facing this issue,I Have as my view <%= form_for(:pin, :url => {:action =>fees})

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.