2013-11-21 36 views
0

我已經添加了一個字符串作爲用戶身份,但無法找到如何從一個類中檢索它。如何從授權憑證中獲取userdata字符串?

FormsAuthenticationTicket tkt; 
string cookiestr; 
HttpCookie ck; 

tkt = new FormsAuthenticationTicket(1, txtUser.Text, DateTime.Now,    
//Amount of minutes for standard session 
DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, UserCompany); 
cookiestr = FormsAuthentication.Encrypt(tkt); 
ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr); 
+0

您注意行:cookiestr = FormsAuthentication.Encrypt(TKT)?它不應該獲取數據。如果可以,騙子也可以。 – lboshuizen

+0

我之前使用過它,但我失去了代碼。在我設法將可訪問的菜單項列表添加爲用戶數據的列表字符串並將其添加到頁面中之前,現在我想將其用於公司ID,因此我無法獲取數據。 – connersz

+0

票不是viewdata,不應該像那樣使用。期。它用於保護您的網站。 – lboshuizen

回答

0

了那裏到底...

 HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
     FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); 


     CompanyID = ticket.UserData;