【问题标题】:Difference between these two Authentication Ticket making ways?这两种Authentication Ticket制作方式的区别?
【发布时间】:2018-01-18 07:57:44
【问题描述】:

我在 MVC 应用程序中遇到了这部分代码

  var authTicket = new FormsAuthenticationTicket(
                                                           1,                             // version
                                                           oModel.UserID,                      // user name
                                                           DateTime.Now,                  // created
                                                           DateTime.Now.AddDays(30),   // expires
                                                           oModel.RememberMe,                   // persistent?
                                                           "Jt_AutoLogin"
                                                           );
                        string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                        var authCookie = new HttpCookie("Jt_AutoLogin", encryptedTicket);
                        if (authTicket.IsPersistent)
                        {
                            authCookie.Expires = authTicket.Expiration;
                        }
                        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);

还有这段代码

FormsAuthentication.SetAuthCookie(oModel.UserID, oModel.RememberMe);

据我所知,SetAuthCookie() 方法会生成经过身份验证的票证并将其添加到 cookie 集合中。

但是第一种方法呢?

如果他们确实都在做同样的事情,那有什么区别?

【问题讨论】:

    标签: asp.net asp.net-mvc forms-authentication


    【解决方案1】:

    他们都工作

    SetAuthCookie 方法将表单身份验证票证添加到 cookie 集合或 URL

    第一个

    var authTicket = new FormsAuthenticationTicket

    允许您向其添加更多用户定义数据,例如过期等。

    https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      相关资源
      最近更新 更多