dru*_*mel 1 authentication ajax asp.net-mvc-3
我有一个AJAX方法来调用服务器返回".ASPXAUTH"cookie到期时间.它在auth cookie出现时正常工作.
此外,我想用另一个AJAX调用续订用户登录会话.我有一个空白的方法"RenewSession",它只是用于调用服务器.有没有办法使用表单身份验证?问题是,当我向服务器发出请求以"RenewSession"方法更新会话时,Response.Cookies数组总是包含0个项目.但实际上当".ASPXAUTH"cookie到期时间达到0时,它会更新.
那么任何人都可以解释它是浏览器还是ASP.NET/MVC的行为?也许我需要将滑动过期设置为"true"?或者也许在我的续订方法中,我应该重新登录用户并在响应中添加新的cookie?
谢谢!
FormsAuthentication到期真的是两个部分:
如果要关闭滑动过期,并手动续订票证,则需要续订票证并将新的身份验证cookie返回给浏览器.
Response.Cookies除非您(或其他代码)向其添加内容,否则该数组为空.它仅用于添加新的 cookie 或其内容/过期/任何已更改的 cookie .空的Response.Cookies只意味着什么都没有改变 - 浏览器将保留它已经拥有的cookie(直到它们到期)并仍然在下一个请求时发送它们.
修改cookie内容或过期的标准方法是获取浏览器发送的cookie(从Request.Cookies),修改它,然后将其添加到Response.Cookies.
这里有一些手动更新身份验证cookie的示例代码(disclamer:彻底测试并思考):
// You could also get the ticket from
// Request.Cookies using FormsAuthentication.Decode
FormsIdentity identity = HttpContext.Current.User.Identity as FormsIdentity;
if (identity == null) return; // User isn't authenticated
// Renew the ticket - you could also create a new ticket manually
// (see * below for an example), if you want to get rid of ASP.NET's
// rather confusing renew-if-old policy:
FormsAuthenticationTicket ticket =
FormsAuthentication.RenewTicketIfOld(identity.Ticket);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket
);
// Better keep this (see * below):
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.HttpOnly = true;
// Isn't a security issue if this is set too long - the ticket contained
// within will still expire after the set time, and the server will timeout
// the auth session on the next request.
// But let's just keep cookie and ticket in sync:
cookie.Expire = ticket.Expiration;
// Add cookie to response to send the changes to the browser:
HttpContext.Current.Response.Cookies.Add(cookie);
Run Code Online (Sandbox Code Playgroud)
请注意,FormsAuthentication.RenewTicketIfOld()并不总是续订票证.只有在剩余的到期时间不到一半时才会续订.即,如果web.config中的超时设置为20分钟并且在创建故障单后7分钟调用RenewTicketIfOld,则不会续订故障单,并且还剩下13分钟.如果在例如12分钟后调用它,它将被更新为20分钟.
原因是因为每次请求都由slidingExpiration使用RenewTicketIfOld,因此会在每个请求上发回一个新的cookie(将到期时间重置为[timeout]分钟).只有在至少一半的时间过去后才发送新的票证cookie才能消除大量的cookie开销 - 这会让开发人员和最终用户感到困惑.
*)On cookie.Secure,请参阅Hanselman:奇怪的超时 - 这只是确保如果在web.config中设置了RequireSSL,那么cookie将尊重它,如果您将网站移动到SSL,这可以避免许多调试噩梦.
| 归档时间: |
|
| 查看次数: |
2494 次 |
| 最近记录: |