zoukankan      html  css  js  c++  java
  • 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");
            }
    }
  • 相关阅读:
    scrapy安装教程
    【bzoj4200】[Noi2015]小园丁与老司机 STL-map+dp+有上下界最小流
    【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分
    【bzoj4198】[Noi2015]荷马史诗 贪心+堆
    【bzoj2989】数列 KD-tree+旋转坐标系
    【bzoj4212】神牛的养成计划 Trie树+可持久化Trie树
    【bzoj4242】水壶 BFS+最小生成树+倍增LCA
    【bzoj4238】电压 DFS树
    【bzoj4240】有趣的家庭菜园 贪心+树状数组
    【bzoj4237】稻草人 分治+单调栈+二分
  • 原文地址:https://www.cnblogs.com/DotaSteam/p/5468080.html
Copyright © 2011-2022 走看看