浅谈MVC Form认证

发布时间:2020-05-29 06:06:55 作者:zddnd
来源:网络 阅读:424

简单的谈一下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

浅谈MVC Form认证

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

浅谈MVC Form认证

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

浅谈MVC Form认证

 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: LogOut15         public ActionResult LogOut()16         {17             FormsAuthentication.SignOut();18             return RedirectToAction("Index", "Login");19         }20     }

浅谈MVC Form认证

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

浅谈MVC Form认证

1     public class MainController : BaseController2     {3         // GET: Main4         public ActionResult Index()5         {6             return View();7         }8     }

浅谈MVC Form认证

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

浅谈MVC Form认证

 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             else13             {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                  else22                  { }23              */24             }25         }26     }

浅谈MVC Form认证

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

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


推荐阅读:
  1. 浅谈加密技术
  2. ASPNetCore MVC ModelValidation-ajax

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

认证 fo

上一篇:学习云计算前景如何?云计算在物联网中到底起到什么作用?

下一篇:Oracle RAC错误之--oifcfg错误案例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》