zoukankan      html  css  js  c++  java
  • C#Mvc登录功能

    首先要建立一个MyWebApp类:

    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); 
    }
    }
    }

    Login View部分:

    @model Droplets.Models.Admin
    @{
    Layout = null;
    ViewBag.Title = "Login";
    }
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> 用户登录 </title>
    <link href="~/Content/icon.css" rel="stylesheet" />
    <link href="~/Content/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.easyui.min.js"></script>
    <script src="~/Scripts/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript">
    
    //if (top.location != window.location)
    //{
    // top.location = window.location;
    
    //}
    function login() {
    $('#form1').submit();
    }
    function cancel()
    {
    $('#form1').reset();
    }
    </script>
    </head>
    <body style="font-size:11px;">
    <div>
    <div id="win" class="easyui-dialog" buttons="#dialogButtons" title="用户登录" style="360px;height:240px; text-align:center;">
    <form style="padding:10px;" id="form1" method="post" action="LoginPost">
    <div style="padding:10px;">
    <label>用户名:</label> 
    <input type="text" class="easyui-textbox" name="name" required="true">
    </div>
    <div style="padding:10px;">
    <label>密&nbsp;码:</label>
    <input type="password" name="password" class="easyui-textbox" required="required"></p>
    </div>
    
    </form>
    </div>
    <div style="padding:5px;" id="dialogButtons">
    <a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="login()">确定</a>
    <a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="cancel()">取消</a>
    
    </div>
    </div>
    </body>
    </html>

     ManagementController部分:

    public class ManagementController : AppController
    {
    //
    // GET: /Management/
    Droplets.Models.DropletsEntities db;
    
    public ManagementController()
    {
    db = new DropletsEntities(); 
    }
    
    protected override void Dispose(bool disposing)
    {
    if (disposing)
    {
    db.Dispose();
    }
    base.Dispose(disposing);
    }
    
    public ActionResult Login()
    {
    return View();
    }
    [HttpPost]
    public ActionResult LoginPost(Admin admin)
    {
    
    var m = db.Admin.FirstOrDefault(t => t.Name == admin.Name && t.PassWord == admin.PassWord);
    if (m == null)
    {
    return View("Login");//如果为NULL怎返回登录页面
    }
    else//否则进入Index页面
    {
    WebCode.MyWebApp.currentUser = m;
    return Redirect("/Management/Index");
    }
    }
    
     
  • 相关阅读:
    系统安全
    导出csv文件示例
    MsChart在MVC下的问题
    记录一些测试的结果
    使用CTE减少统计子查询
    otl获得sql出错位置(oracle)
    在sql语句中使用plsql变量
    Java经典编程题50道之二十四
    Java经典编程题50道之二十三
    Java经典编程题50道之二十二
  • 原文地址:https://www.cnblogs.com/DotaSteam/p/5445041.html
Copyright © 2011-2022 走看看