C# 在将表单发送到后端之前,如何将密码字符串转换为Base64字符串?

C# 在将表单发送到后端之前,如何将密码字符串转换为Base64字符串?,c#,asp.net,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc 4,Razor,我有一个注册表单示例,它在大多数情况下都能正常工作,但当我尝试使用密码“U8$G#CBj”注册新用户时,我收到一个异常“一个潜在危险的请求。从客户端检测到表单值” 我的想法是在将密码发送到后端之前将其转换为Base64格式,然后在后端将其转换回Base64格式。我怎么做 @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class

我有一个注册表单示例,它在大多数情况下都能正常工作,但当我尝试使用密码“U8$G#CBj”注册新用户时,我收到一个异常“一个潜在危险的请求。从客户端检测到表单值” 我的想法是在将密码发送到后端之前将其转换为Base64格式,然后在后端将其转换回Base64格式。我怎么做

@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
     {
        @Html.AntiForgeryToken()
        <h4>Use a local account to log in.</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
           @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
           <div class="col-md-10">
              @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
              @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
           </div>
        </div>
        <div class="form-group">
           @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
           <div class="col-md-10">
              @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
              @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
           </div>
        </div>
        <div class="form-group">
           <div class="col-md-offset-2 col-md-10">
              <div class="checkbox">
                 @Html.CheckBoxFor(m => m.RememberMe)
                 @Html.LabelFor(m => m.RememberMe)
              </div>
           </div>
        </div>
        <div class="form-group">
           <div class="col-md-offset-2 col-md-10">
              <input type="submit" value="Log in" class="btn btn-default" />
           </div>
        </div>
        <p>
           @Html.ActionLink("Register as a new user", "Register")
        </p>
        @* Enable this once you have account confirmation enabled for password reset functionality *@
        <p>
           @Html.ActionLink("Forgot your password?", "ForgotPassword")
        </p>
     }
@使用(Html.BeginForm(“Login”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
使用本地帐户登录。

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(m=>m.Email,新的{@class=“col-md-2控制标签”}) @TextBoxFor(m=>m.Email,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Email,“,new{@class=“text danger”}) @LabelFor(m=>m.Password,新的{@class=“col-md-2控制标签”}) @Html.PasswordFor(m=>m.Password,新的{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Password,“,new{@class=“text danger”}) @CheckBoxFor(m=>m.RememberMe) @LabelFor(m=>m.RememberMe) @ActionLink(“注册为新用户”、“注册”)

@*为密码重置功能启用帐户确认后启用此选项*@ @ActionLink(“忘记密码?”,“放弃密码”)

}
试试这个

1) base64(编码/解码)

2) EncodePasswordMd5

@使用名称空间

在前端部分和后端部分的帮助下,我解决了我的问题,代码如下: 在查看表单时,我在提交事件中添加了这个javascript函数

function encode(){
        $('#Password').val(btoa($('#Password').val()));
        $('#ConfirmPassword').val(btoa($('#ConfirmPassword').val()));
    }
并在后端将字符串解码回:

private string DecodeFromBase64(string inputBas64)
{
    var base64EncodedBytesPassword = System.Convert.FromBase64String(model.Password);
    string password = System.Text.Encoding.UTF8.GetString(base64EncodedBytesPassword);
    return password;
}

感谢您的快速回复,我会尝试它将帮助。在后端这是有用的,但我需要转换前端网站上的字符串
  using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
function encode(){
        $('#Password').val(btoa($('#Password').val()));
        $('#ConfirmPassword').val(btoa($('#ConfirmPassword').val()));
    }
private string DecodeFromBase64(string inputBas64)
{
    var base64EncodedBytesPassword = System.Convert.FromBase64String(model.Password);
    string password = System.Text.Encoding.UTF8.GetString(base64EncodedBytesPassword);
    return password;
}