验证 Windows Phone 应用内购买收据

2023-12-20

我正在开发 Windows Phone 8 应用程序。我的应用程序将包含应用内购买。我试图理解收据的概念。据我了解,当有人在我的应用程序内购买产品后,会生成收据。

<?xml version="1.0"?>
<Receipt Version="1.0" CertificateId="{Identifier1}" xmlns="http://schemas.microsoft.com/windows/2012/store/receipt">
  <ProductReceipt PurchasePrice="${PurchaseAmount}" PurchaseDate="{DateTime}" Id="{Guid1}" AppId="{Guid2}" ProductId="{ProductName}" ProductType="Consumable" PublisherUserId="{Identifier2}" PublisherDeviceId="{Identifier3}" MicrosoftProductId="{Guid3}" />
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
      <Reference URI="">
        <Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></Transforms>
    <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
    <DigestValue>{Identifier4}</DigestValue>
      </Reference>
    </SignedInfo>

    <SignatureValue>{HashedValue}</SignatureValue>
  </Signature>
</Receipt>

伟大的!我不知道如何判断这张收据是否来自微软的服务器。有人可以向我解释如何验证吗?我看到了这个:http://code.msdn.microsoft.com/wpapps/In-app-purchase-receipt-c3e0bce4 http://code.msdn.microsoft.com/wpapps/In-app-purchase-receipt-c3e0bce4然而,这对我来说没有意义。我不明白示例中的证书。 “IapReceiptProduction.cer”是一个固定的东西吗?或者只是为了这个样本?

如果这是一个愚蠢的问题,我很抱歉。


“Receipt”XML 元素中的“CertificateId”属性确定使用哪个证书来签署 Windows 应用商店收据。获得证书 ID(示例中的“{Identifier1}”)后,您可以从下面代码示例中指定为“certificateUrl”的 URL 下载所需的证书。您可以通过以下方式以编程方式下载证书:

public static X509Certificate2 RetrieveCertificate(string certificateId)
{
    const int MaxCertificateSize = 10000;

    // We are attempting to retrieve the following url. The getAppReceiptAsync website at 
    // http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.store.currentapp.getappreceiptasync.aspx
    // lists the following format for the certificate url.
    String certificateUrl = String.Format("https://go.microsoft.com/fwlink/?LinkId=246509&cid={0}", certificateId);

    // Make an HTTP GET request for the certificate
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(certificateUrl);
    request.Method = "GET";

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Retrieve the certificate out of the response stream
    byte[] responseBuffer = new byte[MaxCertificateSize];
    Stream resStream = response.GetResponseStream();
    int bytesRead = ReadResponseBytes(responseBuffer, resStream);

    if (bytesRead < 1)
    {
        //TODO: Handle error here
    }

    return new X509Certificate2(responseBuffer);
}

您可以查看此代码示例的更多内容here http://msdn.microsoft.com/en-ie/library/windows/apps/jj649137.aspx。该示例中包含的“IapReceiptProduction.cer”只是为了展示收据验证的工作原理,而无需通过代码下载证书。拿到证书后就可以使用System.Security.Cryptography.Xml.SignedXml http://msdn.microsoft.com/en-us/library/system.security.cryptography.xml.signedxml(v=vs.71).aspx用于验证收据的 API,如您链接的代码示例中所示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

验证 Windows Phone 应用内购买收据 的相关文章

随机推荐