- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using WMRMOBJSLib;
- namespace Microunit.DRM
- {
- public class LicenseGenerator
- {
- /// <summary>
- /// 生成License
- /// </summary>
- /// <param name="Challenge">页面传过来的Challenge值</param>
- /// <returns></returns>
- public static string Generate(string Challenge)
- {
- //从配置文件内获得私钥、公钥、种子和后发放License获取URL地址
- string privateKey = ConfigurationManager.AppSettings["PrivateKey"];
- string publicKey = ConfigurationManager.AppSettings["PublicKey"];
- string seed = ConfigurationManager.AppSettings["Seed"];
- //处理许可请求对象
- WMRMChallenge myWMRMChallenge = new WMRMChallenge();
- //管理内容头对象
- WMRMHeader myWMRMHeader = new WMRMHeader();
- //管理加密密钥和许可密钥种子对象
- WMRMKeys myWMRMKeys = new WMRMKeys();
- //指定加密内容的使用权限对象
- WMRMRights myWMRMRights = new WMRMRights();
- //创建许可对象
- WMRMLicGen myWMRMLicGen = new WMRMLicGen();
- //将许可递交给客户端对象
- WMRMResponse myWMRMResponse = new WMRMResponse();
- try
- {
- myWMRMChallenge.Challenge = Challenge;
- myWMRMHeader.Header = myWMRMChallenge.Header;
- if (myWMRMHeader.Verify(publicKey) == 0) {
- return "";
- }
- myWMRMKeys.KeyID = myWMRMHeader.KeyID;
- myWMRMKeys.Seed = seed;
- //设置版权
- //最小的安全级别
- myWMRMRights.MinimumSecurityLevel = 150;
- //是否允许播放,false-不允许,true-允许
- myWMRMRights.AllowPlay = true;
- //是否允许在PC机上播放,0-不允许,1-允许
- myWMRMRights.AllowPlayOnPC = 1;
- //允许播放次数
- myWMRMRights.Playcount = 2;
- //是否允许拷贝,false-不允许,true-允许
- myWMRMRights.AllowCopy = true;
- myWMRMRights.AllowTransferToNonSDMI = 1;
- myWMRMRights.AllowTransferToSDMI = 1;
- //是否允许刻录,false-不允许,true-true
- myWMRMRights.AllowPlaylistBurn = false;
- //是否允许备份许可证,0-不允许,1-允许
- myWMRMRights.AllowBackupRestore = 0;
- ////当客户端机器时间更改到更早时间时,该证书是否失效,0-不失效,1-失败
- myWMRMRights.DisableOnClockRollback = 0;
- ////当客户端机器时间更改到更早时间时,该证书是否自动删除,0-不删除,1-删除
- myWMRMRights.DeleteOnClockRollback = 0;
- //添加版权信息
- myWMRMLicGen.KeyID = myWMRMKeys.KeyID;
- myWMRMLicGen.SetKey("", myWMRMKeys.GenerateKey());
- myWMRMLicGen.Rights = myWMRMRights.GetAllRights();
- myWMRMLicGen.ClientInfo = myWMRMChallenge.ClientInfo;
- myWMRMLicGen.Priority = 10;
- object varCategory = null;
- object varVersion = null;
- myWMRMLicGen.GetClientVersion(ref varCategory,ref varVersion);
- switch ((int)varCategory) {
- case 0:
- //Client is an Un-Individualized Win32 System
- //If varVersion is 513 then this is the V7 client
- case 1:
- //Client is an Individualized Win32 System
- //If varVersion is 513 then this is the V7 client
- case 2:
- //Client is a portable device
- default:
- return "";
- }
- //版权
- myWMRMLicGen.set_Attribute("LICENSESERVER", "<NAME>Microunit</NAME>");
- myWMRMLicGen.BindToPubKey = publicKey;
- //生成License发放响应
- string license = myWMRMLicGen.GetLicenseToDeliver();
- myWMRMResponse.AddLicense("2.0.0.0", license);
- return myWMRMResponse.GetLicenseResponse();
- }
- catch (Exception e)
- {
- return "";
- }
- }
- }
- }