zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
  • 相关阅读:
    Bone Collector HDU
    Super Jumping! Jumping! Jumping! HDU
    147. 对链表进行插入排序
    C++ Program to Implement Sorted Circularly Doubly Linked List
    344. 反转字符串
    1360. 日期之间隔几天
    剑指 Offer 62. 圆圈中最后剩下的数字
    1441. 用栈操作构建数组
    594. 最长和谐子序列
    836. 矩形重叠
  • 原文地址:https://www.cnblogs.com/anuo/p/5062534.html
Copyright © 2011-2022 走看看