2012-10-23 121 views
0

我需要使用FormsAuthentication來驗證用戶使用ajax的登錄,我知道我不能使用FormsAuthentication.RedirectFromLoginPage(「UserName」,false),因爲使用ajax時沒有重定向。
我可以使用什麼設置該用戶的授權。
儘量選用:如何在ASP.NET webforms中使用FormsAuthentication時禁用或啓用Cookie?

FormsAuthentication.SetAuthCookie("UserName", false); 

但這裏有餅乾,我不希望使用的cookie。

由於

回答

0

可以從請求/響應循環(例如餅乾)分開使用表單身份驗證的加密/解密/呼氣方面。

例如:你可以得到驗證令牌像這樣的字符串:

// create a ticket for "myusername" which expires in 15 minutes 
string authToken = FormsAuthentication.Encrypt(new FormsAuthenticationTicket("myusername", false, 15)); 

然後在使用時收到它從一些自定義的方法,用戶回來(你真的不指定你」你將使用FormsAuthentication.Decrypt來檢查它是否有效。請注意,爲了檢查有效性,您必須捕獲異常檢查爲空。

// 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) 
相關問題