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

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

   1:  <authentication mode="Forms">
   2:  <forms loginurl="~/Authentication/Login"></forms>
   3:  </authentication>


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

   1:  [Authorize]
   2:  public ActionResult Index()