asp.net - 这两种Authentication Ticket制作方式的区别?

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

我在 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 集合中。

但是第一种方式呢?

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

最佳答案

它们都有效

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

关于asp.net - 这两种Authentication Ticket制作方式的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316336/

相关文章:

c# - 在 WebAPI 客户端中每次调用创建一个新的 HttpClient 的开销是多少?

jquery - 将 jquery 添加到空 MVC 项目

asp.net - 防止登录用户访问asp.net mvc 5中的登录页面

mysql - 如何使用Asp.net从MySql数据库获取salt?

c# - 是否 (HttpContext.Current.User != null) 足以假设 FormsAuthentication 已对用户进行身份验证

asp.net - 内联样式会导致额外的请求

c# - 我的 IF 逻辑有什么问题?

ASP.NET url MAX_PATH 限制

c# - MVC 应用程序中 ViewBag 的空值不一致

asp.net-mvc - ActionFilterAttribute mvc 需要调用一次