2011-06-03 27 views
0

我有一個身份驗證cookie,我想知道是否可以從代碼的asp.net部分中的cookie中提取數據(即在HTML部分)。我可以使用下面的代碼在後端將它拉出來從asp.net前端的cookie中提取細節

IPrincipal p = HttpContext.Current.User; 
      // p.Identity.Name : this is what we will use to call the stored procedure to get the data and populate it 


      string userid = p.Identity.Name; 
      Response.Write("Welcome " + 
       p.Identity.Name); 
      var dbcontext = new PrepLicensingSolution2010.DAL.LicensingEntities1(); 
      if (userid != null) 
      { 
       var user = dbcontext.commissions_proc(userid); 
      } 

但是這可以在前端完成嗎?

感謝

+0

您是否在尋找JavaScript代碼? – Xaqron 2011-06-03 20:31:39

+0

乍一看很奇怪的任務(我是關於cookies的使用)。看起來你想要人爲地解決你的問題。爲什麼你不想使用cookie?請解釋一下,什麼是源代碼問題。讓我們根據你想要實現的場景(我想<用戶類型>應該有可能<做某事>)。 – 2011-06-04 07:55:48

回答

1

這取決於如何設置auth cookie。如果使用HTTPOnly,則不能通過JavaScript或(如果瀏覽器是安全的)客戶端訪問它。

使用在JavaScript中cookie中只使用

var myCookieArray = document.cookie.split(";");

並遍歷數組找到你要找的餅乾。

更多的信息在這裏:http://www.w3schools.com/JS/js_cookies.asp

0

喜歡的東西

<asp:Label ID="welcome" runat="server" Text='<%= String.Format("Welcome {0}", HttpContext.Current.User.Identity.Name) %>' 

唯一的事情就是內容一定要小心進行測試,看看用戶是否允許頁面加載,因爲你可以得到一個NullReferenceException如果之前實際登錄Identity.Name爲空