在asp.net mvc中加密密码时插入数据库失败

时间:2015-10-23 17:21:43

标签: c# asp.net asp.net-mvc-4 encryption

我正在使用ASP.Net mvc开发应用程序。我有一个注册表单。当用户输入密码时,我想加密密码并存储在数据库中。我加密时代码失败密码并尝试将其插入数据库。请查看我的代码,如果有问题请告诉我。

在插入期间,我遇到了错误:  "值不能为null。参数名称:输入" 我的控制器是:

  using CTCFremont.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace CTCFremont.Controllers
{
    public class UserLoginController : Controller
    {
        private CTCFremontEntities db = new CTCFremontEntities();

        //
        // GET: /UserLogin/Create

        public ActionResult Index()
        {

            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /UserLogin/Create

        [HttpPost]
        public ActionResult Create(UserLogin userlogin)
        {
            if (ModelState.IsValid)
            {
                userlogin.Pass = Crypto.SHA256(userlogin.Pass);
                db.UserLogins.Add(userlogin);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(userlogin);
        }



        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

我的观点是:

@model CTCFremont.Models.UserLogin

@{
    ViewBag.Title = "SignUp";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<h2>Sign Up!</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset >
        <legend>Submit your details </legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field" >
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field" >
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Pass)
        </div>
        <div class="editor-field" >
            @Html.EditorFor(model => model.Pass, new { id = "Password" })
            @Html.ValidationMessageFor(model => model.Pass)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Confirm)
        </div>
        <div class="editor-field" >
            @Html.EditorFor(model => model.Confirm, new { id = "Confirmation" })
            @Html.ValidationMessageFor(model => model.Confirm)
        </div>

        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

1 个答案:

答案 0 :(得分:0)

您缺少SQL字符串参数的单引号,需要字符串格式化程序,并且需要使用ExecuteSqlCommand而不是SqlQuery。这应该有用。

db.UserLogins.ExecuteSqlCommand(
  string.Format("insert into Userlogin values('{0}','{1}','{2}')",
    userlogin.UserName, userlogin.Email, EncodedPassowrd));