当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

隐藏ASP中的MediaPlayer控件播放文件地址

  在播放视频时,直接查看页面源文件可看到视频文件地址,MediaPlayer控件播放器将在页面上加上如下初始化语句:

  MediaPlayer控件初始化可查看到源文件地址

  Sys.Application.add_init(function()

  {

  $create(Sys.UI.Silverlight.MediaPlayer,

  {

  "autoPlay":true,"mediaSource":"http://211.142.31.235/Nas1/VOD/CP0001/490Kbps/D/DI/DIA/DiaoZhongYuan1/DiaoZhongYuan1.wmv",

  "scaleMode":2,"source":"UserControl/playerTheme/player.xaml","volume":1},

  null, null,

  $get("myUserMediaPlayer_player_parent"));

  });

  解决方案是先建立一个字符串加密解密服务

  加密、解密工具类

  1/**//// <summary>

  2    /// 对字符串进行、加解密的工具类

  3    /// </summary>

  4    public class DESEncryptor

  5  {

  6        静态公共方法#region 静态公共方法

  7        /**//// <summary>

  8        /// 对字符串进行加密

  9        /// </summary>

  10        /// <param name="strSource">源字符串</param>

  11        /// <returns>加密后的字符串</returns>

  12        public static string  ExecEncrypt(string strSource)

  13        {

  14            DESEncryptor desEncryptor = new DESEncryptor() ;

  15            desEncryptor.InputString = strSource ;

  16            desEncryptor.DesEncrypt() ;

  17            return desEncryptor.OutString ;

  18        }

  19        /**//// <summary>

  20        /// 对已经加密的字符串进行解密

  21        /// </summary>

  22        /// <param name="strSource">已经加密的字符串</param>

  23        /// <returns>还原后的字符串</returns>

  24        public static string  ExecDeEncrypt(string strSource)

  25        {

  26            DESEncryptor desEncryptor = new DESEncryptor() ;

  27            desEncryptor.InputString = strSource ;

  28            desEncryptor.DesDecrypt() ;

  29            return desEncryptor.OutString ;

  30        }

  31        #endregion

  32

  33        私有成员#region 私有成员

  34        /**//// <summary>

  35        /// 输入字符串

  36        /// </summary>

  37        private string inputString=null;

  38        /**//// <summary>

  39        /// 输出字符串

  40        /// </summary>

  41        private string outString=null;

  42        /**//// <summary>

  43        /// 输入文件路径

  44        /// </summary>

  45        private string inputFilePath=null;

  46        /**//// <summary>

  47        /// 输出文件路径

  48        /// </summary>

  49        private string outFilePath=null;

  50        /**//// <summary>

  51        /// 加密密钥

  52        /// </summary>

  53        private string encryptKey="fanmenglife";

  54        /**//// <summary>

  55        /// 解密密钥

  56        /// </summary>

  57        private string decryptKey = "fanmenglife";

  58        /**//// <summary>

  59        /// 提示信息

  60        /// </summary>

  61        private string noteMessage=null;

  62        #endregion

  63

  64        公共属性#region 公共属性

  65        /**//// <summary>

  66        /// 输入字符串

  67        /// </summary>

  68        public string InputString

  69        {

  70            get{return inputString;}

  71            set{inputString=value;}

  72        }

  73        /**//// <summary>

  74        /// 输出字符串

  75        /// </summary>

  76        public string OutString

  77        {

  78            get{return outString;}

  79            set{outString=value;}

  80        }

  81        /**//// <summary>

  82        /// 输入文件路径

  83        /// </summary>

  84        public string InputFilePath

  85        {

  86            get{return inputFilePath;}

  87            set{inputFilePath=value;}

  88        }

  89        /**//// <summary>

  90        /// 输出文件路径

  91        /// </summary>

  92        public string OutFilePath

  93        {

  94            get{return outFilePath;}

  95            set{outFilePath=value;}

  96        }

  97        /**//// <summary>

  98        /// 加密密钥

  99        /// </summary>

  100        public string EncryptKey

  101        {

  102            get{return encryptKey;}

  103            set{encryptKey=value;}

  104        }

  105        /**//// <summary>

  106        /// 解密密钥

  107        /// </summary>

  108        public string DecryptKey

  109        {

  110            get{return decryptKey;}

  111            set{decryptKey=value;}

  112        }

  113        /**//// <summary>

  114        /// 错误信息

  115        /// </summary>

  116        public string NoteMessage

  117        {

  118            get{return noteMessage;}

  119            set{noteMessage=value;}

  120        }

  121        #endregion

  122

  123        构造函数#region 构造函数

  124        /**//// <summary>

  125        /// 构造函数

  126        /// </summary>

  127        public DESEncryptor()

  128        {

  129            //

  130            // TODO: 在此处添加构造函数逻辑

  131            //

  132        }

  133        #endregion

  134

  135        DES加密字符串#region DES加密字符串

  136        /**//// <summary>

  137        /// 加密字符串

  138        /// 注意:密钥必须为8位

  139        /// </summary>

  140        public void DesEncrypt()

  141        {

  142            byte[] byKey=null;

  143            byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

  144            try

  145            {

  146                byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0,8));

  147                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

  148                byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);

  149                MemoryStream ms = new MemoryStream();

  150                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;

  151                cs.Write(inputByteArray, 0, inputByteArray.Length);

  152                cs.FlushFinalBlock();

  153                this.outString=Convert.ToBase64String(ms.ToArray());

  154            }

  155            catch(System.Exception error)

  156            {

  157                this.noteMessage=error.Message;

  158            }

  159        }

  160        #endregion

  161

  162        DES解密字符串#region DES解密字符串

  163        /**//// <summary>

  164        /// 解密字符串

  165        /// </summary>

  166        public void DesDecrypt()

  167        {

  168            byte[] byKey = null;

  169            byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};

  170            byte[] inputByteArray = new Byte[this.inputString.Length];

  171            try

  172            {

  173                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));

  174                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

  175                inputByteArray = Convert.FromBase64String(this.inputString);

  176                MemoryStream ms = new MemoryStream();

  177                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

  178                cs.Write(inputByteArray, 0, inputByteArray.Length);

  179                cs.FlushFinalBlock();

  180                System.Text.Encoding encoding = new System.Text.UTF8Encoding();

  181                this.outString=encoding.GetString(ms.ToArray());

  182            }

  183            catch(System.Exception error)

  184            {

  185                this.noteMessage=error.Message;

  186            }

  187        }

  188        #endregion

  189}

  加密、解密WebService,调用工具内加密、解密方法:

  1/**//// <summary>

  2    /// Summary description for Service1

  3    /// </summary>

  4    [WebService(Namespace = "http://tempuri.org/")]

  5    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

  6    [System.ComponentModel.ToolboxItem(false)]

  7    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

  8    // [System.Web.Script.Services.ScriptService]

  9    public class Service1 : System.Web.Services.WebService

  10    {

  11        /**//// <summary>

  12        /// 加密

  13        /// </summary>

  14        /// <param name="url">地址</param>

  15        /// <returns></returns>

  16        [WebMethod]

  17        public string Encrypt(string url)

  18        {

  19            return DESEncryptor.ExecEncrypt(url);

  20        }

  21

  22        /**//// <summary>

  23        /// 解密

  24        /// </summary>

  25        /// <param name="url">地址</param>

  26        /// <returns></returns>

  27        [WebMethod]

  28        public string DeEncrypt(string url)

  29        {

  30            return DESEncryptor.ExecDeEncrypt(url);

  31        }

  32

  }

  在MediaPlayer控件播放页面加载时,先调用WebService内加密方法对播放地址进行加密,然后注册JavaScript脚本调用视频文件播放方法:

  播放页面加载

  protected void Page_Load(object sender, EventArgs e)

  {

  if (!IsPostBack)

  {

  //对播放地址进行加密

  string encryptUrl = local.Encrypt(playUrl);

  //注册脚本,调用javascript播放方法

  Page.ClientScript.RegisterStartupScript(this.GetType(), "", " setTimeout(\"onPlay('" + encryptUrl + "')\",1000)", true);               }

  }

  JavaScript调用WebService通过微软提供的WebService.htc实现,ASP.NET中使用WebService.htc需要在HTML代码中添加如下代码:

  <div id="serviceDiv" style="behavior: url(WebService.htc)"></div> 注意:该div必需在forms之外

  Javascript代码:

  1 function getUrl(str) {

  2     //第一个参数是webservice的url,后面是名称

  3     serviceDiv.useService("http://localhost:88/Service1.asmx?WSDL", "news");

  4     //设置一个回调函数,service返回结果的时候回调;第一个参数是回调函数的名称,后面的是webservice的参数

  5     intCallID = serviceDiv.news.callService(url_result, "DeEncrypt", str); //这里有两个参数..

  6 }

  7

  8 function url_result(result)//回调函数

  9 {

  10     var media_player = $find('myUserMediaPlayer_player');

  11     media_player.set_mediaSource(result.value);

  12     media_player.set_autoPlay(true);

  13 }

  14

  15 function onPlay(str) {

  16     getUrl(str);

  17 }

  有几点需说明:

  1. 为什么在Page_Load事件里注册脚本不直接调用Javascript内播放方法,因为页面需完全加载后,WebService.htc才会被初始化,所以我用setTimeout方法延迟一段时间再调用播放方法。

  2. 大家在使用WebService回调函数时记得判断返回结果是否成功。

  通过以上方法,在页面上查看源文件便无法查看到播放文件地址了,MediaPlayer初始化语句更新为如下:

  更改后MediaPlayer控件初始化

  Sys.Application.add_init(function()

  {

  $create(Sys.UI.Silverlight.MediaPlayer,

  {"autoPlay":true,"scaleMode":1,"source":"../UserControl/playerTheme/VIPPlayer.xaml","volume":1},

  null, null, $get("ump_player_parent"));

  });

  不过这只是争对一般用户,如果大家有更好的方法可以拿出来一起共享,希望我的文章对大家有帮助。

相关内容
赞助商链接