LicenseGenerator.cs
上传用户:azhen587
上传日期:2022-05-28
资源大小:2k
文件大小:5k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using WMRMOBJSLib;
  5. namespace Microunit.DRM
  6. {
  7.     public class LicenseGenerator
  8.     {
  9.         /// <summary> 
  10.         /// 生成License 
  11.         /// </summary> 
  12.         /// <param name="Challenge">页面传过来的Challenge值</param>
  13.         /// <returns></returns> 
  14.         public static string Generate(string Challenge)
  15.         {
  16.             //从配置文件内获得私钥、公钥、种子和后发放License获取URL地址 
  17.             string privateKey = ConfigurationManager.AppSettings["PrivateKey"];
  18.             string publicKey = ConfigurationManager.AppSettings["PublicKey"];
  19.             string seed = ConfigurationManager.AppSettings["Seed"];
  20.             //处理许可请求对象 
  21.             WMRMChallenge myWMRMChallenge = new WMRMChallenge();
  22.             //管理内容头对象 
  23.             WMRMHeader myWMRMHeader = new WMRMHeader();
  24.             //管理加密密钥和许可密钥种子对象 
  25.             WMRMKeys myWMRMKeys = new WMRMKeys();
  26.             //指定加密内容的使用权限对象 
  27.             WMRMRights myWMRMRights = new WMRMRights();
  28.             //创建许可对象 
  29.             WMRMLicGen myWMRMLicGen = new WMRMLicGen();
  30.             //将许可递交给客户端对象 
  31.             WMRMResponse myWMRMResponse = new WMRMResponse();
  32.             try
  33.             {
  34.                 myWMRMChallenge.Challenge = Challenge;
  35.                 myWMRMHeader.Header = myWMRMChallenge.Header;
  36.                 if (myWMRMHeader.Verify(publicKey) == 0) {
  37.                     return "";
  38.                 }
  39.                 myWMRMKeys.KeyID = myWMRMHeader.KeyID;
  40.                 myWMRMKeys.Seed = seed;
  41.                 //设置版权
  42.                 //最小的安全级别
  43.                 myWMRMRights.MinimumSecurityLevel = 150;
  44.                 //是否允许播放,false-不允许,true-允许 
  45.                 myWMRMRights.AllowPlay = true;
  46.                 //是否允许在PC机上播放,0-不允许,1-允许 
  47.                 myWMRMRights.AllowPlayOnPC = 1;
  48.                 //允许播放次数 
  49.                 myWMRMRights.Playcount = 2;
  50.                 //是否允许拷贝,false-不允许,true-允许 
  51.                 myWMRMRights.AllowCopy = true;
  52.                 myWMRMRights.AllowTransferToNonSDMI = 1;
  53.                 myWMRMRights.AllowTransferToSDMI = 1;
  54.                 //是否允许刻录,false-不允许,true-true 
  55.                 myWMRMRights.AllowPlaylistBurn = false;
  56.                 //是否允许备份许可证,0-不允许,1-允许 
  57.                 myWMRMRights.AllowBackupRestore = 0;
  58.                 ////当客户端机器时间更改到更早时间时,该证书是否失效,0-不失效,1-失败 
  59.                 myWMRMRights.DisableOnClockRollback = 0; 
  60.                 ////当客户端机器时间更改到更早时间时,该证书是否自动删除,0-不删除,1-删除 
  61.                 myWMRMRights.DeleteOnClockRollback = 0;  
  62.                 //添加版权信息
  63.                 myWMRMLicGen.KeyID = myWMRMKeys.KeyID;
  64.                 myWMRMLicGen.SetKey("", myWMRMKeys.GenerateKey());
  65.                 myWMRMLicGen.Rights = myWMRMRights.GetAllRights();
  66.                 myWMRMLicGen.ClientInfo = myWMRMChallenge.ClientInfo;
  67.                 myWMRMLicGen.Priority = 10;
  68.                 object varCategory = null;
  69.                 object varVersion = null;
  70.                 myWMRMLicGen.GetClientVersion(ref varCategory,ref varVersion);
  71.                 switch ((int)varCategory) { 
  72.                     case 0:
  73.                         //Client is an Un-Individualized Win32 System
  74.                         //If varVersion is 513 then this is the V7 client
  75.                     case 1:
  76.                         //Client is an Individualized Win32 System
  77.                         //If varVersion is 513 then this is the V7 client
  78.                     case 2:
  79.                         //Client is a portable device
  80.                     default:
  81.                         return "";
  82.                 }
  83.                 //版权
  84.                 myWMRMLicGen.set_Attribute("LICENSESERVER", "<NAME>Microunit</NAME>");
  85.                 myWMRMLicGen.BindToPubKey = publicKey;
  86.                 //生成License发放响应
  87.                 string license = myWMRMLicGen.GetLicenseToDeliver();
  88.                 myWMRMResponse.AddLicense("2.0.0.0", license);
  89.                 return myWMRMResponse.GetLicenseResponse();
  90.             }
  91.             catch (Exception e)
  92.             {
  93.                 return "";
  94.             }
  95.         }
  96.     }
  97. }