zoukankan      html  css  js  c++  java
  • 浅谈MVC Form认证

    简单的谈一下MVC的Form认证。

    在做MVC项目时,用户登录认证需要选用Form认证时,我们该怎么做呢?下面我们来简单给大家说一下。

    首先说一下步骤

    1、用户登录时,如果校验用户名密码通过后,需要调用FormsAuthentication.SetAuthCookie()这个方法。

    2、用户退出时,需要调用FormsAuthentication.SignOut();方法

    3、在配置文件web.config中,system.web 节点下, 配置<authentication  mode="Forms"/>

    4、校验:HttpContext.User.Identity.IsAuthenticated,如果是false,则没有通过认证,如果是true,则通过了认证

    以上这三部,即可完成用户登录的Form认证了。

    好了,下面我们来看一下具体的代码。(View中的代码就不贴了,只贴Controller中的代码吧)

    1、建立一个用于用户登录用的Model

    1     public class LoginViewModel
    2     {
    3         [DisplayName("用户名")]
    4         public string UserName { get; set; }
    5         [DisplayName("密码")]
    6         public string Password { get; set; }
    7     }

    2、建立登录用的Controller与页面,其中Controller里面有登录与退出两个Action

     1     public class LoginController : Controller
     2     {
     3         // GET: Login
     4         public ActionResult Index(LoginViewModel loginViewModel)
     5         {
     6             if (loginViewModel.UserName == "admin" && loginViewModel.Password == "123456")
     7             {
     8                 FormsAuthentication.SetAuthCookie(loginViewModel.UserName, false);
     9                 return RedirectToAction("Index", "Main");
    10             }
    11             return View();
    12         }
    13 
    14         //GET: LogOut
    15         public ActionResult LogOut()
    16         {
    17             FormsAuthentication.SignOut();
    18             return RedirectToAction("Index", "Login");
    19         }
    20     }

    3、建立一个登录后,用户跳转的页面与Controller

    1     public class MainController : BaseController
    2     {
    3         // GET: Main
    4         public ActionResult Index()
    5         {
    6             return View();
    7         }
    8     }

    4、登陆后跳转的页面的Controller是继承的BaseController,那么BaseController是怎么写的呢?

     1     public class BaseController : Controller
     2     {
     3         protected override void OnActionExecuting(ActionExecutingContext filterContext)
     4         {
     5             base.OnActionExecuting(filterContext);
     6             //登录认证处理
     7             if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
     8             {
     9                 //未登录
    10                 Response.Redirect("~/Login/Index");
    11             }
    12             else
    13             {
    14                 //已登录,Action级权限控制处理
    15                 var controllerName = filterContext.RouteData.Values["controller"].ToString();//控制器名称
    16                 var actionName = filterContext.RouteData.Values["action"].ToString(); //Action名称
    17                 //根据controllerName与actionName进行权限检查
    18                 /*
    19                  if()
    20                  { }
    21                  else
    22                  { }
    23              */
    24             }
    25         }
    26     }

    这个BaseController很简单,大体的作用就是,方式继承这个BaseController的控制器,当执行其下面的Action时,会进行Form校验,如果校验成功,则……,如果校验不成功则……,

    登陆后的页面的Controller都会继承BaseController,这样,就不用在每个Controller中的Action重复的写Form认证的代码了。

    是不是很简单?

    当然,具体的细节问题这里都没有涉及到,这里只是简单的给大家介绍一下Form认证的使用,具体的细节问题,大家可以参考园中的大神们的博文。

  • 相关阅读:
    Picture Control点击事件
    在C/C++中获取可执行文件的图标和信息
    C++获取系统图标方法
    C++ Vector 使用总结
    C++中vector和list的区别
    STL STD::list使用说明
    演示My97 DatePicker过程中的错误
    HTML5的语法变化
    利用 ACE 来实现 UDP 通讯
    VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法
  • 原文地址:https://www.cnblogs.com/xiaomowang/p/6956685.html
Copyright © 2011-2022 走看看