C#-Mvc-修改密码

同样的MyWebApp代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Droplets.Models;
using System.Web;

namespace Droplets.WebCode
{
public static class MyWebApp
{
private const string SessionUser = "CurrentUser";//当前用户
public const string LoginUrl = "/Management/Login";//登录界面
public const string LogoutUrl = "/Management/Logout";//退出登录

public static Admin currentUser
{
get
{
if (HttpContext.Current == null) return null;
return HttpContext.Current.Session[SessionUser] as Admin;
}
set
{
HttpContext.Current.Session[SessionUser] = value;
}
}
public static Admin checkLogin()
{
var user = currentUser;
if (user == null)
{
HttpContext.Current.Response.Redirect(LoginUrl);
}
return user;
}
public static void logout()
{
HttpContext.Current.Session.Remove(SessionUser); 
}
}
}

View部分:

@model Droplets.Models.Admin
@using Droplets.Models
@using Droplets.WebCode
@{
    Layout = null;
    Admin admin = MyWebApp.currentUser;
    string message = ViewBag.message;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/easyui.css" rel="stylesheet" />
    <link href="~/Content/icon.css" rel="stylesheet" />
    <link href="~/Content/demo.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.easyui.min.js"></script>
    <title>修改密码</title>
    <script type="text/javascript">

        function submitForm() {
            var oldpass = $('#oldpass').val();
            var newpass = $('#newpass').val();
            var newpass1 = $('#newpass1').val();
            if (newpass != newpass1) {
                alert('两次输入的新密码不一致!')
                return false;
            }
            if (oldpass == newpass && newpass == newpass1)
            {
                alert('新密码不能和旧密码相同!')
                return false;
            }

            $('#form1').submit();
        }
        function reset(){
            $('#form1').form('clear');
        }


    </script>
    @if (!string.IsNullOrEmpty(message))
    {
        <script type="text/javascript">
            $(function() {
                alert("@message");
            });
        </script>
    }
    <style type="text/css">
        #div1 {
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -150px 0 0 -200px;
            border: 1px solid #008800;
        }
    </style>
</head>
<body>
    <div id="div1">
        <div class="easyui-panel" title="密码修改" style="500px;padding:30px 60px">
            <div style="margin-bottom:30px">
                <form method="post" id="form1" action="/Admin/Index">
                    <table>
                        <tbody>
                            <tr style="margin-bottom:20px">
                                <td><label>用户名:</label></td>
                                <td><lable>@admin.Name</lable></td>
                            </tr>
                            <tr style="margin-bottom:20px">
                                <td><lable>旧密码:</lable></td>
                                <td><input class="easyui-textbox" id="oldpass" type="password" name="oldpass"></td>
                            </tr>
                            <tr style="margin-bottom:20px">
                                <td><lable>新密码:</lable></td>
                                <td><input class="easyui-textbox" id="newpass" type="password" name="newpass"></td>
                            </tr>
                            <tr style="margin-bottom:20px">
                                <td><lable>确认新密码:</lable></td>
                                <td><input class="easyui-textbox" id="newpass1" type="password" name="newpass1"></td>
                            </tr>
                        </tbody>
                    </table>
                    <div style="margin-top:30px">
                        <a href="#" class="easyui-linkbutton" onclick="submitForm()" style="20%;margin-left:10%">提交</a>
                        <a href="#" class="easyui-linkbutton" onclick="reset()" style="20%;margin-left:10%">重置</a>
                        @*<input type="submit" value="提交" style="60px;height:30px;margin-right:50px" />
                            <input type="reset" value="取消" style="60px;height:30px" />*@
                    </div>
                </form>
            </div>
        </div>
    </div>

</body>
</html>

AdminController部分:

using System.Web.Mvc; //要引入的
public class AdminController : Controller
    {
        //
        // GET: /Admin/
        Droplets.Models.DropletsEntities db;

        public AdminController()
        {
            db = new DropletsEntities();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        public ActionResult Index()
        {
            var admin = MyWebApp.checkLogin();
            if (admin == null)
            {
                return RedirectToAction("/Managemnet/Login");
            }
            return View();
        }
        [HttpPost]
        [ActionName("Index")]
        public ActionResult IndexPost()
        {
            string AdminName = MyWebApp.currentUser.Name;
            var temp = db.Admin.FirstOrDefault(t => t.Name == AdminName);
            string pas = temp.PassWord;
            if (pas != Request.Form["oldpass"])
            {
                ViewBag.message = "旧密码输入错误";
               
            }
            else {
                temp.PassWord = Request.Form["newpass"];
                db.SaveChanges();
                ViewBag.message = ("密码修改成功!");
            }
            return View("Index");
        }
}
原文地址:https://www.cnblogs.com/DotaSteam/p/5468080.html