MVC系列-10.用户验证-导航条改造

1.实现Form认证

打开 Web.config文件,在System.Web部分,找到Authentication的子标签。如果不存在此标签,就在文件中添加Authentication标签。

设置Authentication的Mode为Forms,loginUrl设置为”Login”方法的URL

  <authentication mode="Forms">

       <forms loginUrl="~/Account/Login"></forms>

   </authentication>

2.让Action 方法更安全--改造某些需要登录才能用的方法

在 某些 action 方法中添加认证属性 [Authorize].

例如 index

wps7E17.tmp

3改造action--login

(1)在accountcontroller里添加引用

using System.Web.Security;

(2)在login里增加保存验证状态的语句

wps7E28.tmp

4.在View 中显示信息

打开Login View,在 @Html.BeginForm前面 添加以下代码-显示错误信息

   @Html.ValidationMessage("CredentialError", new {style="color:red;" })

wps7E29.tmp

5.注销功能

在controller里新增action

public ActionResult Logout()

  {

FormsAuthentication.SignOut();

return RedirectToAction("Login");

   }

6.导航条显示用户名

(1)新建显示用户名、注册、登陆的、分部视图_LoginPartial

wps7E3A.tmp

(2)在布局页引用分布视图--mylayout

wps7E3B.tmp

posted @ 2016-06-06 13:10  chenglingr  阅读(157)  评论(0编辑  收藏  举报
生活在继续,我们需要活着