SetLicenseResponse Method
Collapse the table of content
Expand the table of content

LicenseAcquirer.SetLicenseResponse Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Sets the license response by passing the SOAP body from the HTTP response to the license challenge.

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)

protected void SetLicenseResponse(
	Stream licenseResponse


Type: System.IO.Stream
The SOAP body from the HTTP response to the license challenge. The SOAP body is passed in as a stream and contains the entire SOAP response.


The licenseResponse is null.


The specified licenseResponse is invalid.

If the LicenseAcquirer is created on demand (from the media element) and if this method is called with null, the MediaElement transitions from the AcquiringLicense state to the error state and raises the MediaFailed event corresponding to that.

If the LicenseAcquirer is created on demand (from the media element) and if you are handling license acquisition on your own by overriding OnAcquireLicense, you are expected to call this method before the MediaElement leaves the AcquiringLicense state.

The LicenseAcquirer class is used by the MediaElement to handle acquiring licenses for DRM encrypted content from a PlayReady License Server. You can subclass the LicenseAcquirer class and add custom logic like adding your own custom authentication scheme to the license request.

The following example shows how to override the LicenseAcquirer class (named "ManualLicenseAcquirer") and have a MediaElement use it to acquire the license.

<StackPanel x:Name="LayoutRoot" Background="Gray" Orientation="Vertical">
   <MediaElement x:Name="myME" Height="100"/>
public partial class Page : UserControl

    public Page()
      this.Loaded += new RoutedEventHandler(Page_Loaded);

    void Page_Loaded(object sender, RoutedEventArgs e)
      // Test a full fledged manual acquirer

      // Set the LicenseAcquirer of the MediaElement to the custom License Acquirer
      // defined in this sample.
      myME.LicenseAcquirer = new ManualLicenseAcquirer(myME.Name);

      // Set the License URI to proper License Server address.
      myME.LicenseAcquirer.LicenseServerUriOverride = new Uri("", UriKind.Absolute);
      myME.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(myME_MediaFailed);

      // Set the source of the MediaElement to the URL of the media encrypted with WMDRM.
      myME.Source = new Uri("", UriKind.Absolute);

    void myME_MediaFailed(object sender, ExceptionRoutedEventArgs e)
      string errorMessage = "";
      if (e.ErrorException.ToString().Contains(" 6001 "))
          errorMessage = "The individualization component software failed to" +
                         " download to the user’s computer. This error would" +
                         " come up when the MediaElement is in the Individualizing" +
                         " MediaElementState. One possible reason for this error is" +
                         " that Windows Phone cannot connect the Microsoft" +
                         " Individualization Server.";
      else if (e.ErrorException.ToString().Contains(" 6004 "))
          errorMessage = " The installation of Windows Phone on the device is" +
                         " out of date and needs to be updated.";  
          errorMessage = "MediaFailed: " + e.ErrorException.Message + ".";

    // makes license request explicitly
    public class ManualLicenseAcquirer : LicenseAcquirer
      private string challengeString;
      string _mediaElementName;

    public ManualLicenseAcquirer(string mediaElementName)
      _mediaElementName = mediaElementName;

    // The default implementation of OnAcquireLicense calls into the MediaElement to acquire a
    //  license. It is called when the Media pipeline is building a topology and will be raised
    // before MediaOpened is raised.
    protected override void OnAcquireLicense(System.IO.Stream licenseChallenge, Uri licenseServerUri)
      StreamReader sr = new StreamReader(licenseChallenge);
      challengeString = sr.ReadToEnd();

      // Need to resolve the URI for the License Server -- make sure it is correct
      // and store that correct URI as resolvedLicenseServerUri.
      Uri resolvedLicenseServerUri;
      if (LicenseServerUriOverride == null)
        resolvedLicenseServerUri = licenseServerUri;
        resolvedLicenseServerUri = LicenseServerUriOverride;

      // Make a HttpWebRequest to the License Server.
      HttpWebRequest request = WebRequest.Create(resolvedLicenseServerUri) as HttpWebRequest;
      request.Method = "POST";

      // Set ContentType through property    
      request.ContentType = "application/xml";

      // The headers below are necessary so that error handling and redirects are handled 
      // properly via Windows Phone.
      request.Headers["msprdrm_server_redirect_compat"] = "false";
      request.Headers["msprdrm_server_exception_compat"] = "false";

      //  Initiate getting request stream  
      IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), request);

    // This method is called when the asynchronous operation completes.
    void RequestStreamCallback(IAsyncResult ar)
      HttpWebRequest request = ar.AsyncState as HttpWebRequest;

      // populate request stream  
      request.ContentType = "text/xml";
      Stream requestStream = request.EndGetRequestStream(ar);
      StreamWriter streamWriter = new StreamWriter(requestStream, System.Text.Encoding.UTF8);


      // Make async call for response  
      request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);

    private void ResponseCallback(IAsyncResult ar)
      HttpWebRequest request = ar.AsyncState as HttpWebRequest;
      WebResponse response = request.EndGetResponse(ar);

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft