加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用flash cookie 保存用户信息(MVC)兼容各种主流浏览器

发布时间:2020-12-15 18:29:46 所属栏目:百科 来源:网络整理
导读:最近在做一个项目,其中要用flash cookie 保存用户的信息,至于flash cookie 就不再介绍了,前面已提过 由于对as(actionscript)一点都不懂,摸索了好长时间,才写出一个简单的例子,涉及到的知识点有: 环境:window 2008 、flex builder3.0 1.as 语法 2.Sha

最近在做一个项目,其中要用flash cookie 保存用户的信息,至于flash cookie 就不再介绍了,前面已提过

由于对as(actionscript)一点都不懂,摸索了好长时间,才写出一个简单的例子,涉及到的知识点有:

环境:window 2008 、flex builder3.0

1.as 语法

2.SharedObject 对象

3.flex 与js 通信

4.swfobject.js

我把代码贴出来,和大家分享一下(跨域的代码没测试,所以注释掉了,测试之后再来补充)

testFCookie.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
	applicationComplete="initApp()" width="500" height="500" backgroundGradientAlphas="[1.0,1.0]">
	<mx:Script>
		<![CDATA[
			import flash.external.*;
			//js跨域访问
			//flash.system.Security.allowDomain("192.168.0.135:913","localhost:913");
			//flash.system.Security.loadPolicyFile("crossdomain.xml");
			            
      		private var cookieFlag:String = 'cookie';     		          
      		
      		          
			//保存web项目中的值(保存flash cookie)
            public function callWrapper():String{
            	var f:String = "changeDocumentTitle";
            	var getJSValue:String="";
            	try{
            		getJSValue=ExternalInterface.call(f,"new title");
            		var cookieSharedObj:SharedObject=SharedObject.getLocal(cookieFlag);
            		cookieSharedObj.data.text=getJSValue;
            		cookieSharedObj.flush();
            	}catch(ex:Error){
            	}
            	
            	return getJSValue;
            }
            
      		//取flash cookie中的值
      		public function asFunc():String{
      			var strValue:String='';
      			var getCookieShareObj:SharedObject=SharedObject.getLocal(cookieFlag);
      			if(getCookieShareObj.size>0)
      			{
      				strValue=getCookieShareObj.data.text;
      			}
      			else
      			{
      				strValue=callWrapper();
      			}
      			return strValue;
      		}
      		
      		//返回flash cookie中的值
      		public function initApp():void {
        		//AddCallback方法允许javascript调用flash时间上函数 
          		ExternalInterface.addCallback("flexFunctionAlias",asFunc);
      		}
      		
		]]>
	</mx:Script>
</mx:Application>

index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <base target="_self"/>
    <title>默认程序运行后自动写入一个flash cookie</title>
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="expires" content="0"/>
    <script language="JavaScript" type="text/javascript">
        function findSWF(movieName) {
            if (navigator.appName.indexOf("Microsoft") != -1) {
                return window[movieName];
            } else {
                return document[movieName];
            }
        }
       
        //接收来自as的值
        function callApp() {
            try{
                var x = findSWF("MyFlexApp").flexFunctionAlias();
                $("#receivedBField").val(x);
                $.post("http://localhost:913/Home/Decrypt",{ "fcookie": x },function (data) {
                    $("#receivedField").val(data);
                });
            }catch(e){
                $("#receivedField").val("");
            }
        }

        //发送至as
        function changeDocumentTitle(a) {
            window.document.title = a;
            return '@ViewBag.Message'
        }
    </script>
</head>
<body style='overflow-x: hidden; overflow-y: hidden'>
    <form name="htmlForm">
    网页已自动将”flash cookie,20“这个值写入到flash cookie中
    <br />
    <br />
    取已保存的flash cookie的值:<br />
    解密前的值:<input type="text" id="receivedBField"/><br />
    解密后的值:<input type="text" id="receivedField"/>
    <input type="button" value="接收" onclick="callApp();" /><br />
    <div id="flashcontent">
        <object id="MyFlexApp" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0" width="100%" height="500px">
            <param name="movie" value="testFCookie.swf">
            <param name="quality" value="high">
            <param name="scale" value="noborder">
            <param name="bgcolor" value="#000000">
            <param name="wmode" value="transparent">
            <param name="allowScriptAccess" value="allways" /> 
            <embed src="testFCookie.swf" quality="high" width="100%" height="500" scale="noborder" allowscriptaccess="always"
                bgcolor="#000000" name="TH2" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
        </object>
    </div>
    </form>
</body>
</html>

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcFlashCookie.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            ViewBag.Message = Common.strHelper.EncryptMD5("flash cookie,20");
            return View();
        }

        [HttpPost]
        public ContentResult  Decrypt()
        {
            string strResult = string.Empty;
            if (!string.IsNullOrEmpty(Request.Form["fcookie"]))
            {
                strResult = Common.strHelper.DecryptMD5(Request.Form["fcookie"]);
            }
            return  Content(strResult);
        }
    }
}

以上是完整的代码

经测试发现:这个例子只能在IE、360下跑。在其他浏览器中全跑不通。在网上查各种资料,结果测试一边一边的都不行,快要放弃的时候看到这样一篇文章:解决不同浏览器下各种JS与AS通信失败问题(利用swfobject开源JS工程)。他的例子没看到,我就直接查了swfobject.js的用法。改了之后,在各个浏览器(火狐、IE、360、safari、opera、谷歌)中都行了:更改的代码如下:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <base target="_self"/>
    <title>默认程序运行后自动写入一个flash cookie</title>
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="expires" content="0"/>
    <script src="../../Scripts/swfobject.js" type="text/javascript"></script>
    <script language="JavaScript" type="text/javascript">

        //接收来自as的值
        function callApp() {
            
            try{
                var x =document.getElementById("MyFlexApp").flexFunctionAlias();
                $("#receivedBField").val(x);
                $.post("http://localhost:913/Home/Decrypt",20“这个值写入到flash cookie中
    <br />
    <br />
    取已保存的flash cookie的值:<br />
    解密前的值:<input type="text" id="receivedBField"/><br />
    解密后的值:<input type="text" id="receivedField"/>
    <input type="button" value="接收" onclick="callApp();" /><br />
    <div id="flashcontent">
    </div>
    <script type="text/javascript">
        var so = new SWFObject("testFCookie.swf","MyFlexApp","100%","500","7","#336699");
        so.addParam("quality","low");
        so.addParam("wmode","transparent");
        so.addParam("salign","t");
        so.write("flashcontent");
    </script>
    </form>
</body>
</html>

这样就顺利解决了

说明:

一、flash cookie 保存位置
谷歌flash cookie 保存的位置:
"C:UsersAdministratorAppDataLocalGoogleChromeUser DataDefault

Pepper DataShockwave FlashWritableRootmacromedia.comsupport

flashplayersys#localhostsettings.sol"

火狐、IE、360、safari、opera保存的位置:
"C:UsersAdministratorAppDataRoamingMacromediaFlash Player

#SharedObjectsRBGKDDCW#localhosttestFCookie.swfcookie.sol"

二、
1.IE8、IE9在删除历史记录的时候会清空flash cookie

2.谷歌21 删除历史记录的时候会清空flash cookie

?

以上是完整的代码,如果有人不想复制粘贴,有源码下载,但是要分哦:

flash cookie

我是一个初学者,从什么都不懂到琢磨出一个例子,有不足的地方 请大家见谅。也请高手指点

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读