ASP.NET MVC 身份认证之Forms认证

原文地址:https://www.cnblogs.com/anuo/p/5062534.html





ASP.NET MVC 身份认证

身份认证的好处就是, 如果这个页面没有登录, 刷新后会自动跳到登录页要求登录,保证了应用程序的安全。而Forms 身份认证是web下最常用的,如何配置呢?见下(基于mvc 4)

1.在webconfig,<system.web>节点下加如下配置

<authentication mode="Forms">
  <forms loginUrl="~/Login"/>
</authentication>

 

2.配置RouteConfig,将defaults 配置为从Login启动,这样启动页就是登录页了

复制代码
  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
            );
        }
复制代码

3.编写FormsAuth 身份认证类

复制代码
 public class FormsAuth
    {
        public static void SignIn()
        {
            //创建一个FormsAuthenticationTicket,它包含登录名以及额外的用户数据。
            var ticket = new FormsAuthenticationTicket(2,
                "anuodog", DateTime.Now, DateTime.Now.AddDays(1), true, "密码:123");

            //加密Ticket,变成一个加密的字符串。
            var cookieValue = FormsAuthentication.Encrypt(ticket);

            //根据加密结果创建登录Cookie
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue)
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Domain = FormsAuthentication.CookieDomain,
                Path = FormsAuthentication.FormsCookiePath
            };

            cookie.Expires = DateTime.Now.AddMinutes(20);

            var context = HttpContext.Current;

            //写登录Cookie
            context.Response.Cookies.Remove(cookie.Name);
            context.Response.Cookies.Add(cookie);
        }

        public static void SingOut()
        {
            FormsAuthentication.SignOut();
        }

    }
复制代码

4. 在LoginController 里面调用FormsAuth 类中的登入登出方法

复制代码
   public class LoginController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DoLogin()
        {
            FormsAuth.SignIn();

            return Json("success");
        }

        public ActionResult DoLogout()
        {
            FormsAuth.SingOut();

            return Json("success");
        }

    }
复制代码

5.在项目App_Start文件夹中找到 FilterConfig类 ,并添加一个配置,请看下面代码注释

复制代码
 public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());//新添加此配置的作用是给所有Action方法都加了个[Authorize]特性,这样,每当访问这个Action时如果没有通过身份认证,将弹跳至登陆页,要求登陆。
        }
    }
复制代码

6.如果想更细粒度的控制,Action的访问,可以去掉5步骤的配置,并在需要控制的Action上 加[Authorize]特性就行了,这样如果访问的这个Action有[Authorize]特性并且又没登陆就会被跳到登陆页,如下:

复制代码
 public class PtypeController : Controller
    {

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

    }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值