Nehal Jain
Nehal Jain

Reputation: 105

How to do Base64 Encoding while posting a html form?

I am stuck at one point where I need to encode the posted URL to Base64 while posting the Form in a cshtml page. We do have enctype property inside form element where we get 3 kinds of encoding, however due to some client requirement we need to encode the URL in Base64. Following is the form element:

<body onload="document.getElementById('idPost').submit()">
    <form id="idPost" method="post" action= @System.Configuration.ConfigurationManager.AppSettings["postUrl"]
        style="margin: 0; padding: 0;">
        <fieldset style="border: 0">
            <h3 style="text-align: center;">We Are Signing You In, Please Wait...</h3>

            @Html.HiddenFor(model => model.agent);
             @Html.HiddenFor(model => model.User_ID);
            @Html.HiddenFor(model => model.Password);
            @Html.HiddenFor(model => model.errorUrl);
            @Html.HiddenFor(model => model.originalResourceUri);

        </fieldset>
    </form>
</body>

Any help in getting this done would be appreciated.

Upvotes: 1

Views: 1847

Answers (1)

Bhuban Shrestha
Bhuban Shrestha

Reputation: 1324

Convert your hidden field to base 64 string as

 @Html.HiddenFor(model => Convert.ToBase64String(Encoding.UTF8.GetBytes(model.agent)));

You can create extension method for same and do like

@Html.HiddenFor(model => model.agent.ToBase64())

Upvotes: 1

Related Questions