How to attach X509Certificate2 to webservice (Apple GSX / C# specific)

给你一囗甜甜゛ 提交于 2019-12-12 05:41:52

问题


Apple released their New Generation WSDL on the 15 of August this year (2015) and the big change was that every call to the WSDL had to be validated with a certificate file.

I've done the process to get the certificate from Apple, and I've whitelisted our server IP, and I've even verified that I can get access to the service endpoint from our server by coding a simple interface using HttpWebRequest where I easily can attach the certificate using webRequest.ClientCertificates.Add(), so I know everything is ready for it to work.

But, my problem arises when I download the WSDL from https://gsxwsut.apple.com/apidocs/prod/html/WSArtifacts.html?user=asp

I import the WSDL into Visual Studio and when I try to make an instance of the client class, the only one I find is GsxWSEmeaAspPortClient, which seems to be correct as it has all the functions for authenticate and various tools, but it does not have ClientCertificates. It does have ClientCredentials which have ClientCertificate, but when I try to set the cert there, it's as tho it's never set.

I'm guessing the service code transmits the data via either HttpWebRequest or WebRequest, so if I just can get to the request code from my instance of the class (GsxWSEmeaAspPortClient) I can probably fix it, but I can't seem to get there.

I've looked at this question: How can I connect with Apple's GSX NewGeneration webservices with WCF? which suggests it really should be that easy, but I don't have the GsxWSEmeaAspService, only the GsxWSEmeaAspPortClient from my Visual Studio's generation of the WSDL.

If anyone has any ideas which can point me in any direction towards victory I'd be eternally grateful.

I'm using Visual Studio 2013 and the solution is .Net v4.5.1 if that makes any difference.


回答1:


I've pasted new code here:

public void Authenticate() {
    // Use custom binding with certificate authentication
    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    // Create service endpoint
    // Use proper endpoint address - eg. gsxapi.apple.com for production
    EndpointAddress endpoint = new EndpointAddress("https://gsxapiit.apple.com/gsx-ws/services/emea/asp");
    // Create new service
    Apple.GsxWSEmeaAspPortClient service = new Apple.GsxWSEmeaAspPortClient(binding, endpoint);
    // Set loaded certificate
    service.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(
        "[PathToContainerFromStep7].p12",
        "[YourPasswordFromStep8]");
    // Create authenticate request object
    Apple.authenticateRequestType auth = new Apple.authenticateRequestType()
    {
        languageCode = "en",
        userId = "[YourAppleServiceAccountNumber]",
        userTimeZone = "[YourTimeZone]",
        serviceAccountNo = "[YourSoldToNumber]"
    };
    // Authenticate to Apple GSX
    Apple.authenticateResponseType session = service.Authenticate(auth);
    // Assign your new session id object
    userSessionId = new Apple.gsxUserSessionType() { userSessionId = session.userSessionId };
}


来源:https://stackoverflow.com/questions/32565590/how-to-attach-x509certificate2-to-webservice-apple-gsx-c-sharp-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!