【发布时间】:2015-10-23 17:21:43
【问题描述】:
我正在使用 ASP.Net mvc 开发应用程序。我有一个注册表单。当用户输入密码时,我想加密密码并存储在数据库中。当我加密密码时我的代码失败并且尝试将其插入数据库。请查看我的代码,如果有错误请告诉我。
在插入期间,我遇到了错误: “值不能为空。参数名称:输入” 我的控制器是:
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")
}
【问题讨论】:
-
您是否收到任何错误消息要与我们分享?
-
谢谢史蒂夫。我更新了我的帖子
-
您的表 userlogin 是否仅包含三列?如果您使用 INSERT 而不指定列名,那么您应该为所有列传递值。
-
你读过msdn.microsoft.com/en-us/library/…关于那个SHA1类的内容吗?
-
我想指出,SHA-1 不是安全密码哈希。 SHA-2 和 SHA-3 也不是。见How to securely hash passwords?。
标签: c# asp.net asp.net-mvc-4 encryption