我的C#代码无法导入p12证书并进行身份验证,并显示错误的无效凭据

时间:2019-05-28 06:31:46

标签: c# .net xml wsdl

我有一个与客户端一起运行的wsdl服务器。我们有2个服务,第一个服务是通过使用提供给我们的用户名和密码进行验证来获取用户令牌。我们能够接收令牌。现在,当我们进入第二项服务时,我们应该传递出口ID,用户ID,提供者ID,然后传递给我们,并且我们应该使用p12或pfx证书(这些证书可在文件夹中获得)和在第二个服务案例中,要验证的用户标识和密码分别是令牌和另一个密码。但是,我们无法调用证书并将其与用户ID,密码和前面提到的3个提供商数据(出口ID,提供商ID和用户ID)一起传递到服务器上。请帮助我们将证书传递给wsdl服务器。当我们使用SoapUi时,我们必须手动添加WS安全配置和密钥库(jks证书),并且在那里可以工作,但是我们如何在c#中做到这一点?

我尝试在SoapUi上运行它,它可以与证书一起正常工作,但是如果没有证书,代码将显示相同的无效凭据错误

using System;

using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };

                ServiceReference1.AuthenticateAndGetUserDetailsResponse responseObject;

                using (ServiceReference1.SecurityPortTypeClient client = new ServiceReference1.SecurityPortTypeClient())
                {
                    ServiceReference1.ItemsChoiceType[] objArray = new ServiceReference1.ItemsChoiceType[2];
                    objArray[0] = ServiceReference1.ItemsChoiceType.UserId;
                    objArray[1] = ServiceReference1.ItemsChoiceType.Password;

                    string[] itemArray = new string[2];

                    itemArray[0] = "AZAE022";
                    itemArray[1] = "LZDE238a";
                    ServiceReference1.AuthenticateAndGetUserDetailsRequest request = new ServiceReference1.AuthenticateAndGetUserDetailsRequest();
                    request.UserLoginDetails = new ServiceReference1.UserLoginDetailsType()
                    {
                        ChannelType = ServiceReference1.ChannelType.PORTAL,
                        ChannelTypeSpecified = true,
                        IPAddress = "192.168.100.26",
                        ItemsElementName = objArray,
                        Items = itemArray
                    };
                    responseObject = client.AuthenticateAndGetUserDetails(request);
                }

                using (HotelService.HotelEventManagementServicePortTypeClient client = new HotelService.HotelEventManagementServicePortTypeClient())
                {
                    HotelService.SubmitHotelEventRequest request = new HotelService.SubmitHotelEventRequest();
                    HotelService.HotelBookingType hotelBookingType = new HotelService.HotelBookingType();
                    hotelBookingType.ProviderData = new HotelService.ProviderDataType() { OutletId = "12355790", ProviderId = "AZAE", UserId = "AZAE022" };
                    hotelBookingType.PersonData = new HotelService.BookingPersonDataType()
                    {
                        Birth = new HotelService.BirthType()
                        {
                            Place = "India",
                            Date = new DateTime(1983, 12, 2)
                        },
                        CountryofResidence = "India",
                        FamilyName = "Family",
                        Gender = HotelService.GenderType.Male,
                        GenderSpecified = true,
                        GivenName = "Name",
                        IdDocumentData = new HotelService.BookingIdDocumentDataType()
                        {
                            DocumentExpiryDate = new DateTime(2022, 12, 31),
                            DocumentExpiryDateSpecified = true,
                            DocumentIssueAuthority = "Government of India",
                            DocumentIssueCountry = "India",
                            DocumentIssueDate = new DateTime(2012, 12, 31),
                            DocumentIssueDateSpecified = true,
                            DocumentIssuePlace = "Mumbai",
                            DocumentNumber = "P483828",
                            DocumentType = HotelService.TraveldocType.P,
                            DocumentTypeSpecified = true,
                            IsEndorsee = false,
                            IsEndorseeSpecified = true
                        },
                        Nationality = "Indian"
                    };
                    hotelBookingType.EventData = new HotelService.BookingEventDataType()
                    {
                        //BookingReference = "12345",
                        //CardType = "VISA",
                        ContactNumber1 = "12345678",
                        ContactNumber2 = "98761532",
                        Email = "abcd@yahoo.com",
                        JobTitle = "Senior Software Consultant",
                        NumberofOccupants = "3",
                        NumberofRooms = "1",
                        OrganisationName = "asdf",
                        OrganisationNumber = "23232323",
                        PaymentDetail = new HotelService.PaymentDetailType[] { new HotelService.PaymentDetailType() { PaymentMethod = HotelService.PaymentMethodType.Cash } },
                        ScheduledArrivalDate = new DateTime(2019, 5, 19),
                        ScheduledDepartureDate = new DateTime(2019, 5, 31)
                    };
                    request.Item = hotelBookingType;

                    string userToken = responseObject.IdentificationToken;

                   client.ClientCredentials.UserName.UserName = userToken;
                    client.ClientCredentials.UserName.Password = "Test@123";

                    HotelService.SubmitHotelEventResponse response = client.SubmitHotelEvent(request);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }

}

错误消息是SYS9003:无效的凭据

0 个答案:

没有答案