2013-04-09 115 views
3

我正在使用Windows Phone 8应用。我的应用将包含应用内购买。我试图理解收据概念。根据我的理解,在用户在我的应用程序内购买产品后,会生成收据。验证Windows Phone应用内购买收据

<?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但是,它对我没有意义。我不明白例子中的证书。 “IapReceiptProduction.cer”是一套吗?或者只是为了这个样本?

对不起,如果这是一个愚蠢的问题。

+0

http://blogs.msdn.com/b/lighthouse/archive/2013/08/28/faqs-about-in-app-purchase-for-windows-phone-8.aspx FYI – 2014-05-29 02:56:47

回答

6

'Receipt'XML元素中的'CertificateId'属性确定使用哪个证书来签署Windows应用商店收据。在您的示例中具有CertificateID(“{Identifier1}”)后,您可以从以下代码示例中指定的URL下载所需的证书作为'certificateUrl'。这是你如何将程序下载证书:

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的。该示例中包含“IapReceiptProduction.cer”,仅显示接收验证如何工作,无需通过代码下载证书。获得证书后,您可以使用System.Security.Cryptography.Xml.SignedXml API验证收据,如链接的代码示例所示。