1.创建数据库 User表:
![](https://img-blog.csdn.net/20170720182549383?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTk2Nzg1Nzk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2. 创建一个Model类 : User1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyApiTest.Models
{
public class User1
{
public string UserName { get; set; }
public string PassWord { get; set; }
public Int32 ID { set; get; }
}
}
3.创建一个控制器: UserAuthenAttribute.cs
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Filters;
using MyApiTest.Models;
namespace MyApiTest.CustomAttributes
{
public class UserAuthenAttribute : FilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
var user = filterContext.HttpContext.Session["User"];
if (user == null)
{
var Url = new UrlHelper(filterContext.RequestContext);
var url = Url.Action("LogState", "Login", new { area = "" });
filterContext.Result = new RedirectResult(url);
}
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
}
}
}
![](https://img-blog.csdn.net/20170720183320115?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMTk2Nzg1Nzk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
4. 创建登陆控制器 LoginController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyApiTest.Models;
using MySql.Data.MySqlClient;
namespace MyApiTest.Controllers
{
public class LoginController : Controller
{
public ActionResult Index()
{
return View();
}
string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
#region State
[HttpGet]
public JsonResult LogState(User1 user)
{
return Json(new { status = 1, msg = "还没登陆" }, JsonRequestBehavior.AllowGet);
}
#endregion
#region Logon
[HttpPost]
public JsonResult Logon(string username, string password)
{
using (MySqlConnection con = new MySqlConnection(strConn))
{
string sqlStr = "select UserName,PassWord,ID from Users where UserName='" + username + "' and PassWord='" + password + "'";
List<User1> list = new List<User1>();
using (MySqlCommand cmd = new MySqlCommand(sqlStr, con))
{
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
User1 model = new User1();
object obj;
obj = dr["UserName"];
if (obj != null && obj != DBNull.Value)
{
model.UserName = (string)obj;
}
model.UserName = dr["UserName"].ToString();
model.PassWord = dr["PassWord"].ToString();
model.ID = Convert.ToInt32(dr["ID"]);
list.Add(model);
}
if (list != null && list.Count != 0)
{
Session["User"] = username;
return Json(new { status = 1, msg = "登陆succssfull", list = list }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { status = 1, msg = "用户名或密码不正确" }, JsonRequestBehavior.AllowGet);
}
}
}
}
#endregion
#region 退出
public ActionResult LogOff()
{
if (Session["User"] != null)
{
Session["User"] = null;
}
return Json(new { status = 1, msg = "退出成功" }, JsonRequestBehavior.AllowGet);
}
#endregion
}
}
5. 最后在需要使用处使用
using MyApiTest.CustomAttributes;
#region Get方法 查询数据 (提取Model,简化使用)
[HttpGet]
[UserAuthen]
public JsonResult getMySQLHandel()
{
return Json(new { status = 1, msg = "获取成功", list = getdata() }, JsonRequestBehavior.AllowGet);
}
#endregion