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

 
原文地址:https://www.cnblogs.com/DotaSteam/p/5445041.html