zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    2020/5/8
    2020/5/8
    2020/5/6
    2020/4/30
    2020/4/29
    2020/4/28
    2020/4/27
    KMP算法详解
    博客搬家声明
    洛谷P2831 NOIP2016 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/lingr/p/5563471.html
Copyright © 2011-2022 走看看