在ASP.NET webforms中使用FormsAuthentication时如何禁用或启用cookie?

在ASP.NET webforms中使用FormsAuthentication时如何禁用或启用cookie?,asp.net,jquery,forms-authentication,Asp.net,Jquery,Forms Authentication,我需要使用FormsAuthentication为使用ajax的用户验证登录,我知道我不能使用FormsAuthentication。RedirectFromLoginPage(“用户名”,false),因为使用ajax时没有重定向。 我可以使用什么设置此用户的授权。 尝试使用: FormsAuthentication.SetAuthCookie("UserName", false); 但这里有饼干,我不想用饼干 谢谢您可以将表单身份验证的加密/解密/过期方面与请求/响应周期(例如cookie

我需要使用FormsAuthentication为使用ajax的用户验证登录,我知道我不能使用FormsAuthentication。RedirectFromLoginPage(“用户名”,false),因为使用ajax时没有重定向。
我可以使用什么设置此用户的授权。
尝试使用:

FormsAuthentication.SetAuthCookie("UserName", false);
但这里有饼干,我不想用饼干


谢谢

您可以将表单身份验证的加密/解密/过期方面与请求/响应周期(例如cookie)分开使用

例如:您可以将auth令牌作为字符串获取,如下所示:

// create a ticket for "myusername" which expires in 15 minutes
string authToken = FormsAuthentication.Encrypt(new FormsAuthenticationTicket("myusername", false, 15));
然后,当使用从用户处接收来自某个自定义方法(您没有真正指定要使用什么而不是cookie)的信息时,您可以使用它来检查它是否有效。请注意,为了检查有效性,您必须捕获异常并检查null

// decrypt receivedAuthToken string which was received somehow in your request
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(receivedAuthToken);
但是,您还必须自己管理更新过程:

var newTicket = FormsAuthentication.RenewTicketIfOld(authTicket);
var newToken = FormsAuthentication.Encrypt(newTicket)