1、配置Web.conf,使用Form认证方式
 
<system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <authentication mode="Forms">
      <forms loginUrl="~/Login/Index" timeout="1440" />
    </authentication>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
  </system.web>
 
2、登陆验证成功后写入用户信息

//登陆
        public ActionResult Index(LoginViewModel model)
        {
           if (db.User.Count(x => x.Account == strUser && x.Password == strPassword && x.Enable == true) > 0)
            {
                //用户验证成功
                FormsAuthentication.SetAuthCookie(strUser, model.Remember);
                return RedirectToAction("Index", "Admin", new { pagesize = 10 });
            }
            else
            {
                return View();
            }
        }
        //退出登陆
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");

        }
 

3、验证登陆及权限检查,需要让所有需要登陆的页面从BaseController派生

    public class BaseController: Controller
    {
        /// <summary>
        /// 重写基类在Action执行之前的操作,统一登陆验证及页面权限验证
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
 
            //登陆验证处理
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //未登陆
                Response.Redirect("~/Login/Index");
            }
            else
            {
                //己登陆,Action级权限控制处理
                string ControllerName = filterContext.RouteData.Values["controller"].ToString();
                string ActionName = filterContext.RouteData.Values["action"].ToString();
                //根据ControllerName 和 ActionName 进行权限检查
                if()
                {}
                else
                {
                    //己登陆,没有权限

                    Response.Redirect("~/Login/Index");
                }
            }
        }
    }
 

4、密码MD5加密

        public static string GetMd5Hash(string input)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

MVC用户登陆验证及权限检查(Form认证)的更多相关文章

  1. 【Java EE 学习 20】【使用过滤器实现登陆验证、权限认证】【观察者模式和监听器(使用监听器实现统计在线IP、登录IP 、踢人功能)】

    一.使用过滤器实现登录验证.权限认证 1.创建5张表 /*使用过滤器实现权限过滤功能*/ /**创建数据库*/ DROP DATABASE day20; CREATE DATABASE day20; ...

  2. MVC简单登陆验证

    配置文件: <system.web> <authentication mode="Forms"> <!-- 如果验证失败就返回URL的指定界面,设置c ...

  3. 2016-02-20WebForm登陆验证,判断用户是否登陆 PageBase类

    http://blog.csdn.net/fanbin168/article/details/49404233 很多时候,WebFrom页面,我们需要判断用户是否已经登陆了.假如有很多页面,难道我们要 ...

  4. form表单使用(博客系统的登陆验证,注册)

    先从小的实例来看form的用法 登陆验证实例,来看form的常规用法 1. forms.py # 用于登陆验证验证 from django.core.validators import RegexVa ...

  5. 1_python小程序之实现用户的注册登陆验证功能

    python小程序之实现用户的注册登陆验证功能 程序扼要简述:  一.程序流程:1.程序开始2.判断本地文件/数据库是否已存在用户信息,存在则跳转到登陆,否则跳转到注册,注册成功后后跳转到登陆3.判断 ...

  6. angularjs中$http、$location、$watch及双向数据绑定学习实现简单登陆验证

    使用$http.$location.$watch及双向数据绑定等实现简单的用户登陆验证,记录备忘: 1.$http模拟从后台获取json格式的数据: 2.$watch实时监控数据变化: 3.$loca ...

  7. Symfony FOSUserBundle用户登录验证

    symfony是一个由组件构成的框架,登录验证的也是由一些组件构成,下面就介绍一下FOSUserBundle的使用. 以symfony 3.3为例, 首先我们需要先安装一下FOSUserBundle. ...

  8. 用户登陆界面(jquery)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证)

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(17)-注册用户功能的细节处理(各种验证) ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框 ...

随机推荐

  1. Maven搭建webService (三) 创建客户端---使用Apache CXF方式实现

    package test; import net.cc.web.server.HelloWorld; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean ...

  2. cf 496B Secret Combination

    题目链接:B. Secret Combination You got a box with a combination lock. The lock has a display showing n d ...

  3. ios 环境配置网址

    http://blog.csdn.net/cwb1128/article/details/18019751

  4. linux - 使用curl实现新浪天气API应用

    新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...

  5. .net 类型源码下载地址

    原文:http://www.cnblogs.com/ProJKY/p/SSCLI.html 一般场景下,采用 Reflector可以反射出.NET 的部分实现出来,可以拿来参考,但和微软公开的SSCL ...

  6. 一道有趣的javascript编程题

    题目:实现以下功能 1. 点击按钮“打开新窗口”,打开新的子页面,要求新窗口的大小为400px X 200px 2. 输入地址信息,点击“确定”按钮,关闭该页面 3. 将子页面中输入的地址信息,回传到 ...

  7. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)

    第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...

  8. 2016PHP开发者大会

    大会干货: Rasmus Lerdorf——<Speeding up the Web with PHP 7> PHP 7 is here. It brings drastic perfor ...

  9. http://www.cnblogs.com/leiOOlei/p/5075402.html

    http://www.cnblogs.com/leiOOlei/p/5075402.html

  10. 问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the view outlet was not set.

    问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the vie ...