.net 的登陆验证接口的实现

1.创建数据库 User表:


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
    {
        //// GET: UserAuthenAttribute
        //public ActionResult Index()
        //{
        //    return View();
        //}

        public void OnAuthentication(AuthenticationContext filterContext)
        {
            //这个方法是在Action执行之前调用
            var user = filterContext.HttpContext.Session["User"];
            if (user == null)
            {
                //filterContext.HttpContext.Response.Redirect("/Account/Logon");
                var Url = new UrlHelper(filterContext.RequestContext);
                var url = Url.Action("LogState", "Login", new { area = "" });
                filterContext.Result = new RedirectResult(url);
            }
        }
        public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
        {
            //这个方法是在Action执行之后调用
        }

    }
}


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
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }
        string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

        #region State
        [HttpGet]
        public JsonResult LogState(User1 user)
        {
            //填充了Session 表示已有数据==已登录
            //Session["User"] = user.UserName;   
            //Session["User"] = "niu";
            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))
            {
                //插入sql语句        
                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 RedirectToAction("Logon");  

            //APP接口的方法,只要返回信息即可
            return Json(new { status = 1, msg = "退出成功" }, JsonRequestBehavior.AllowGet);
        }
     #endregion
    }
}

5. 最后在需要使用处使用

  • 导入引用
using MyApiTest.CustomAttributes;
  • 在使用的方法是上面调用

        #region Get方法  查询数据 (提取Model,简化使用)
        [HttpGet]
        //[CheckLogin]
        [UserAuthen]
        public JsonResult getMySQLHandel()
        {         
          return Json(new { status = 1, msg = "获取成功", list = getdata() }, JsonRequestBehavior.AllowGet);
        }
        #endregion
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小毅哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值