C#将数据保存到数据库并退出程序

时间:2017-02-03 07:25:18

标签: c# azure

Hey Guys我有一个用c#编写的脚本生成一些加密密钥,我想保存到我的数据库中,我的代码如下所示:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.WindowsAzure.MediaServices.Client;
using Microsoft.WindowsAzure.MediaServices.Client.ContentKeyAuthorization;
using Microsoft.WindowsAzure.MediaServices.Client.DynamicEncryption;
using Microsoft.WindowsAzure.MediaServices.Client.Widevine;
using Newtonsoft.Json;


namespace DeliverDRMLicenses
{
    class Program
    {
    // Read values from the App.config file.
    private static readonly string _mediaServicesAccountName =
        ConfigurationManager.AppSettings["MediaServicesAccountName"];
    private static readonly string _mediaServicesAccountKey =
        ConfigurationManager.AppSettings["MediaServicesAccountKey"];

    private static readonly Uri _sampleIssuer =
        new Uri(ConfigurationManager.AppSettings["Issuer"]);
    private static readonly Uri _sampleAudience =
        new Uri(ConfigurationManager.AppSettings["Audience"]);

    // Field for service context.
    private static CloudMediaContext _context = null;
    private static MediaServicesCredentials _cachedCredentials = null;

    static void Main(string[] args)
    {
        // Create and cache the Media Services credentials in a static class variable.
        _cachedCredentials = new MediaServicesCredentials(
                        _mediaServicesAccountName,
                        _mediaServicesAccountKey);
        // Used the cached credentials to create CloudMediaContext.
        _context = new CloudMediaContext(_cachedCredentials);

        bool tokenRestriction = true;
        string tokenTemplateString = null;
        string drm_key_id = null;

        IContentKey key = CreateCommonTypeContentKey();

        // Print out the key ID and Key in base64 string format
        drm_key_id = key.Id;


        Console.WriteLine(" key:{0}", 
            key.Id, System.Convert.ToBase64String(key.GetClearKeyValue()));

        Console.WriteLine(" key value:{1} ",
            key.Id, System.Convert.ToBase64String(key.GetClearKeyValue()));


           sbasedrmdataDataSetTableAdapters.sbase_drm_keysTableAdapter sbasedrmTableAdapter =
             new sbasedrmdataDataSetTableAdapters.sbase_drm_keysTableAdapter();
         sbasedrmTableAdapter.Insert(drm_key_id);


        Console.WriteLine("Protection key: {0} ",
            key.ProtectionKeyId, System.Convert.ToBase64String(key.GetClearKeyValue()));

        Console.WriteLine("PlayReady URL: {0}", 
            key.GetKeyDeliveryUrl(ContentKeyDeliveryType.PlayReadyLicense));

        Console.WriteLine("Widevin URL: {0}",
            key.GetKeyDeliveryUrl(ContentKeyDeliveryType.Widevine));

        if (tokenRestriction)
            tokenTemplateString = AddTokenRestrictedAuthorizationPolicy(key);
        else
            AddOpenAuthorizationPolicy(key);

        Console.WriteLine("Auth policy: {0}", 
            key.AuthorizationPolicyId);
        Console.WriteLine();
        Console.ReadLine();
        Environment.Exit(0);         

    }

static public void AddOpenAuthorizationPolicy(IContentKey contentKey)
    {

        // Create ContentKeyAuthorizationPolicy with Open restrictions 
        // and create authorization policy          

        List<ContentKeyAuthorizationPolicyRestriction> restrictions = 
            new List<ContentKeyAuthorizationPolicyRestriction>
        {
            new ContentKeyAuthorizationPolicyRestriction
            {
                Name = "Open",
                KeyRestrictionType = (int)ContentKeyRestrictionType.Open,
                Requirements = null
            }
        };

        // Configure PlayReady and Widevine license templates.
        string PlayReadyLicenseTemplate = ConfigurePlayReadyLicenseTemplate();

        string WidevineLicenseTemplate = ConfigureWidevineLicenseTemplate();

        IContentKeyAuthorizationPolicyOption PlayReadyPolicy =
            _context.ContentKeyAuthorizationPolicyOptions.Create("",
                ContentKeyDeliveryType.PlayReadyLicense,
                    restrictions, PlayReadyLicenseTemplate);

        IContentKeyAuthorizationPolicyOption WidevinePolicy =
            _context.ContentKeyAuthorizationPolicyOptions.Create("",
                ContentKeyDeliveryType.Widevine,
                restrictions, WidevineLicenseTemplate);

        IContentKeyAuthorizationPolicy contentKeyAuthorizationPolicy = _context.
                    ContentKeyAuthorizationPolicies.
                    CreateAsync("Deliver Common Content Key with no restrictions").
                    Result;


        contentKeyAuthorizationPolicy.Options.Add(PlayReadyPolicy);
        contentKeyAuthorizationPolicy.Options.Add(WidevinePolicy);
        // Associate the content key authorization policy with the content key.
        contentKey.AuthorizationPolicyId = contentKeyAuthorizationPolicy.Id;
        contentKey = contentKey.UpdateAsync().Result;
    }

    public static string AddTokenRestrictedAuthorizationPolicy(IContentKey contentKey)
    {
        string tokenTemplateString = GenerateTokenRequirements();

        List<ContentKeyAuthorizationPolicyRestriction> restrictions = 
            new List<ContentKeyAuthorizationPolicyRestriction>
        {
            new ContentKeyAuthorizationPolicyRestriction
            {
                Name = "Token Authorization Policy",
                KeyRestrictionType = (int)ContentKeyRestrictionType.TokenRestricted,
                Requirements = tokenTemplateString,
            }
        };

        // Configure PlayReady and Widevine license templates.
        string PlayReadyLicenseTemplate = ConfigurePlayReadyLicenseTemplate();

        string WidevineLicenseTemplate = ConfigureWidevineLicenseTemplate();

        IContentKeyAuthorizationPolicyOption PlayReadyPolicy =
            _context.ContentKeyAuthorizationPolicyOptions.Create("Token option",
                ContentKeyDeliveryType.PlayReadyLicense,
                    restrictions, PlayReadyLicenseTemplate);

        IContentKeyAuthorizationPolicyOption WidevinePolicy =
            _context.ContentKeyAuthorizationPolicyOptions.Create("Token option",
                ContentKeyDeliveryType.Widevine,
                    restrictions, WidevineLicenseTemplate);

        IContentKeyAuthorizationPolicy contentKeyAuthorizationPolicy = _context.
                    ContentKeyAuthorizationPolicies.
                    CreateAsync("Deliver Common Content Key with token restrictions").
                    Result;

        contentKeyAuthorizationPolicy.Options.Add(PlayReadyPolicy);
        contentKeyAuthorizationPolicy.Options.Add(WidevinePolicy);

        // Associate the content key authorization policy with the content key
        contentKey.AuthorizationPolicyId = contentKeyAuthorizationPolicy.Id;
        contentKey = contentKey.UpdateAsync().Result;

        return tokenTemplateString;
    }

    static private string GenerateTokenRequirements()
    {
        TokenRestrictionTemplate template = new TokenRestrictionTemplate(TokenType.SWT);

        template.PrimaryVerificationKey = new SymmetricVerificationKey();
        template.AlternateVerificationKeys.Add(new SymmetricVerificationKey());
        template.Audience = _sampleAudience.ToString();
        template.Issuer = _sampleIssuer.ToString();
        template.RequiredClaims.Add(TokenClaim.ContentKeyIdentifierClaim);

        return TokenRestrictionTemplateSerializer.Serialize(template);
    }

    static private string ConfigurePlayReadyLicenseTemplate()
    {
        // The following code configures PlayReady License Template using .NET classes
        // and returns the XML string.

        //The PlayReadyLicenseResponseTemplate class represents the template 
        //for the response sent back to the end user. 
        //It contains a field for a custom data string between the license server 
        //and the application (may be useful for custom app logic) 
        //as well as a list of one or more license templates.

        PlayReadyLicenseResponseTemplate responseTemplate = 
            new PlayReadyLicenseResponseTemplate();

        // The PlayReadyLicenseTemplate class represents a license template 
        // for creating PlayReady licenses
        // to be returned to the end users. 
        // It contains the data on the content key in the license 
        // and any rights or restrictions to be 
        // enforced by the PlayReady DRM runtime when using the content key.
        PlayReadyLicenseTemplate licenseTemplate = new PlayReadyLicenseTemplate();

        // Configure whether the license is persistent 
        // (saved in persistent storage on the client) 
        // or non-persistent (only held in memory while the player is using the license).  
        licenseTemplate.LicenseType = PlayReadyLicenseType.Nonpersistent;

        // AllowTestDevices controls whether test devices can use the license or not.  
        // If true, the MinimumSecurityLevel property of the license
        // is set to 150.  If false (the default), 
        // the MinimumSecurityLevel property of the license is set to 2000.
        licenseTemplate.AllowTestDevices = true;

        // You can also configure the Play Right in the PlayReady license by using the PlayReadyPlayRight class. 
        // It grants the user the ability to playback the content subject to the zero or more restrictions 
        // configured in the license and on the PlayRight itself (for playback specific policy). 
        // Much of the policy on the PlayRight has to do with output restrictions 
        // which control the types of outputs that the content can be played over and 
        // any restrictions that must be put in place when using a given output.
        // For example, if the DigitalVideoOnlyContentRestriction is enabled, 
        //then the DRM runtime will only allow the video to be displayed over digital outputs 
        //(analog video outputs won’t be allowed to pass the content).

        // IMPORTANT: These types of restrictions can be very powerful 
        // but can also affect the consumer experience. 
        // If the output protections are configured too restrictive, 
        // the content might be unplayable on some clients. 
        // For more information, see the PlayReady Compliance Rules document.

        // For example:
        //licenseTemplate.PlayRight.AgcAndColorStripeRestriction = new AgcAndColorStripeRestriction(1);

        responseTemplate.LicenseTemplates.Add(licenseTemplate);

        return MediaServicesLicenseTemplateSerializer.Serialize(responseTemplate);
    }


    private static string ConfigureWidevineLicenseTemplate()
    {
        var template = new WidevineMessage
        {
            allowed_track_types = AllowedTrackTypes.SD_HD,
            content_key_specs = new[]
            {
                new ContentKeySpecs
                {
                    required_output_protection = 
                        new RequiredOutputProtection { hdcp = Hdcp.HDCP_NONE},
                    security_level = 1,
                    track_type = "SD"
                }
            },
            policy_overrides = new
            {
                can_play = true,
                can_persist = true,
                can_renew = false
            }
        };

        string configuration = JsonConvert.SerializeObject(template);
        return configuration;
    }


    static public IContentKey CreateCommonTypeContentKey()
    {
        // Create envelope encryption content key
        Guid keyId = Guid.NewGuid();
        byte[] contentKey = GetRandomBuffer(16);

        IContentKey key = _context.ContentKeys.Create(
                                keyId,
                                contentKey,
                                "ContentKey",
                                ContentKeyType.CommonEncryption);

        return key;
    }



    static private byte[] GetRandomBuffer(int length)
    {
        var returnValue = new byte[length];

        using (var rng =
            new System.Security.Cryptography.RNGCryptoServiceProvider())
        {
            rng.GetBytes(returnValue);
        }

        return returnValue;
        }


    }
}

所以我遇到的问题是当我尝试运行程序时,我在这一行得到错误

sbasedrmTableAdapter.Insert(drm_key_id);

我收到的错误是:

  

错误CS7036没有给出对应的参数   所需的正式参数&#39; drm_key&#39;的   &#39; sbase_drm_keysTableAdapter.Insert(string,string,string,string)&#39;

我该如何解决这个错误

1 个答案:

答案 0 :(得分:1)

看起来你试图调用一个只需要一个参数就需要4个字符串作为参数的方法。尝试为方法提供正确的参数。

您的问题与OOP inheritance and default constructor

非常相似