C# 第一个数据API服务引用抛出HTTP请求未经客户端身份验证方案授权';匿名';
我正在将第一个数据支付网关服务的API集成到我的项目中。直到大约3周前,我的集成还在运行。我们重新启动了服务器,现在集成不再工作。 当将我的应用程序发布到开发机器上时,它可以正常工作,但是我们的生产环境在这一点上与服务不一致 研究错误消息会导致身份验证出现服务问题,但没有服务器凭据发送到应用程序调用的服务。我想;或多或少,这是IIS的一个问题,但我有一段时间试图解决它 我的整合如下 课程C# 第一个数据API服务引用抛出HTTP请求未经客户端身份验证方案授权';匿名';,c#,asp.net,iis,soap,firstdata,C#,Asp.net,Iis,Soap,Firstdata,我正在将第一个数据支付网关服务的API集成到我的项目中。直到大约3周前,我的集成还在运行。我们重新启动了服务器,现在集成不再工作。 当将我的应用程序发布到开发机器上时,它可以正常工作,但是我们的生产环境在这一点上与服务不一致 研究错误消息会导致身份验证出现服务问题,但没有服务器凭据发送到应用程序调用的服务。我想;或多或少,这是IIS的一个问题,但我有一段时间试图解决它 我的整合如下 课程 public class Merchant { private readonly string _g
public class Merchant
{
private readonly string _gatewayId;
private readonly string _password;
private readonly string _keyId;
private readonly string _hmac;
private readonly bool _isDemo;
// POST URLs
private const string ProdUrl = "https://api.globalgatewaye4.firstdata.com/transaction/v14";
private const string TestUrl = "https://api.demo.globalgatewaye4.firstdata.com/transaction/v14";
public Merchant(string gatewayId, string password, string hmac, string keyId, bool isDemo = true)
{
_gatewayId = gatewayId;
_password = password;
_hmac = hmac;
_keyId = keyId;
_isDemo = isDemo;
}
public MerchantResponse Charge(
string orderId, // Order ID / Reference Number
string cardHoldersName, // Card Holder Name
string cardNumber, // Card number
string amount, // Payment amount
string expirationMonth, // Card Exp. Month
string expirationYear, // Card Exp. Year
string ccv, // CCV code
string address, // Address
string zip) // Zip
{
var client = new ServiceSoapClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), new EndpointAddress(_isDemo ? TestUrl : ProdUrl));
client.ChannelFactory.Endpoint.Behaviors.Add(new HmacHeaderBehaivour(_hmac, _keyId));
TransactionResult result = client.SendAndCommit(new Transaction
{
ExactID = _gatewayId,
Password = _password,
Transaction_Type = "00",
Card_Number = cardNumber,
CardHoldersName = cardHoldersName,
DollarAmount = amount,
Expiry_Date = string.Format("{0:D2}{1}", expirationMonth, expirationYear),
Customer_Ref = orderId.ToString(),
//VerificationStr1 = address + "|" + zip + "|" + "US",
//VerificationStr2 = ccv
});
var response = new MerchantResponse
{
IsTransactionApproved = result.Transaction_Approved,
IsError = result.Transaction_Error
};
if (!result.Transaction_Approved && !result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (!result.Transaction_Approved && result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (result.Transaction_Approved)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
return response;
}
class HmacHeaderBehaivour : IEndpointBehavior
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderBehaivour(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new HmacHeaderInspector(_hmac, _keyId));
}
}
class HmacHeaderInspector : IClientMessageInspector
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderInspector(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message msg = buffer.CreateMessage();
ASCIIEncoding encoder = new ASCIIEncoding();
var sb = new StringBuilder();
var xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings
{
OmitXmlDeclaration = true
});
var writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
msg.WriteStartEnvelope(writer);
msg.WriteStartBody(writer);
msg.WriteBodyContents(writer);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
writer.Flush();
string body = sb.ToString().Replace(" />", "/>");
byte[] xmlByte = encoder.GetBytes(body);
SHA1CryptoServiceProvider sha1Crypto = new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(sha1Crypto.ComputeHash(xmlByte)).Replace("-", "");
string hashedContent = hash.ToLower();
//assign values to hashing and header variables
string time = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string hashData = "POST\ntext/xml; charset=utf-8\n" + hashedContent + "\n" + time + "\n/transaction/v14";
//hmac sha1 hash with key + hash_data
HMAC hmacSha1 = new HMACSHA1(Encoding.UTF8.GetBytes(_hmac)); //key
byte[] hmacData = hmacSha1.ComputeHash(Encoding.UTF8.GetBytes(hashData)); //data
//base64 encode on hmac_data
string base64Hash = Convert.ToBase64String(hmacData);
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
}
客户服务参考地址
public class Merchant
{
private readonly string _gatewayId;
private readonly string _password;
private readonly string _keyId;
private readonly string _hmac;
private readonly bool _isDemo;
// POST URLs
private const string ProdUrl = "https://api.globalgatewaye4.firstdata.com/transaction/v14";
private const string TestUrl = "https://api.demo.globalgatewaye4.firstdata.com/transaction/v14";
public Merchant(string gatewayId, string password, string hmac, string keyId, bool isDemo = true)
{
_gatewayId = gatewayId;
_password = password;
_hmac = hmac;
_keyId = keyId;
_isDemo = isDemo;
}
public MerchantResponse Charge(
string orderId, // Order ID / Reference Number
string cardHoldersName, // Card Holder Name
string cardNumber, // Card number
string amount, // Payment amount
string expirationMonth, // Card Exp. Month
string expirationYear, // Card Exp. Year
string ccv, // CCV code
string address, // Address
string zip) // Zip
{
var client = new ServiceSoapClient(new BasicHttpBinding(BasicHttpSecurityMode.Transport), new EndpointAddress(_isDemo ? TestUrl : ProdUrl));
client.ChannelFactory.Endpoint.Behaviors.Add(new HmacHeaderBehaivour(_hmac, _keyId));
TransactionResult result = client.SendAndCommit(new Transaction
{
ExactID = _gatewayId,
Password = _password,
Transaction_Type = "00",
Card_Number = cardNumber,
CardHoldersName = cardHoldersName,
DollarAmount = amount,
Expiry_Date = string.Format("{0:D2}{1}", expirationMonth, expirationYear),
Customer_Ref = orderId.ToString(),
//VerificationStr1 = address + "|" + zip + "|" + "US",
//VerificationStr2 = ccv
});
var response = new MerchantResponse
{
IsTransactionApproved = result.Transaction_Approved,
IsError = result.Transaction_Error
};
if (!result.Transaction_Approved && !result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (!result.Transaction_Approved && result.Transaction_Error)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
if (result.Transaction_Approved)
{
// Format Response String
response.Message =
result.Authorization_Num + "|" + // Authorization Number
result.Transaction_Tag + "|" + // Transaction Tag (Transaction ID)
result.CardHoldersName + "|" + // Cardholder's Name
result.DollarAmount + "|" + // Transaction Amount
result.Customer_Ref + "|" + // Cust. Reference Number
result.EXact_Message + "|" + // Response Message
result.Bank_Message + "|" + // Bank Response Message
result.Card_Number + "|" + // Card Number
result.CardType + "|" + // Card Type
result.ZipCode // Zip Code
;
}
return response;
}
class HmacHeaderBehaivour : IEndpointBehavior
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderBehaivour(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new HmacHeaderInspector(_hmac, _keyId));
}
}
class HmacHeaderInspector : IClientMessageInspector
{
private readonly string _hmac;
private readonly string _keyId;
public HmacHeaderInspector(string hmac, string keyId)
{
_hmac = hmac;
_keyId = keyId;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message msg = buffer.CreateMessage();
ASCIIEncoding encoder = new ASCIIEncoding();
var sb = new StringBuilder();
var xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings
{
OmitXmlDeclaration = true
});
var writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
msg.WriteStartEnvelope(writer);
msg.WriteStartBody(writer);
msg.WriteBodyContents(writer);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
writer.Flush();
string body = sb.ToString().Replace(" />", "/>");
byte[] xmlByte = encoder.GetBytes(body);
SHA1CryptoServiceProvider sha1Crypto = new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(sha1Crypto.ComputeHash(xmlByte)).Replace("-", "");
string hashedContent = hash.ToLower();
//assign values to hashing and header variables
string time = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string hashData = "POST\ntext/xml; charset=utf-8\n" + hashedContent + "\n" + time + "\n/transaction/v14";
//hmac sha1 hash with key + hash_data
HMAC hmacSha1 = new HMACSHA1(Encoding.UTF8.GetBytes(_hmac)); //key
byte[] hmacData = hmacSha1.ComputeHash(Encoding.UTF8.GetBytes(hashData)); //data
//base64 encode on hmac_data
string base64Hash = Convert.ToBase64String(hmacData);
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
httpRequestMessage.Headers["X-GGe4-Content-SHA1"] = hashedContent;
httpRequestMessage.Headers["X-GGe4-Date"] = time;
httpRequestMessage.Headers["Authorization"] = "GGE4_API " + _keyId + ":" + base64Hash;
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
}
服务参考的配置
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceSoap">
<security mode="Transport" />
</binding>
<binding name="ServiceSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://api.globalgatewaye4.firstdata.com/transaction/v14"
binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
contract="FirstDataReference.ServiceSoap" name="ServiceSoap" />
</client>
</system.serviceModel>
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
服务器堆栈跟踪:
HTTP请求未经客户端身份验证方案“匿名”授权。从服务器接收的身份验证标头为“”
位于System.ServiceModel.Channels.HttpChannelUtilities.ValidateAuthentication(HttpWebRequest请求、HttpWebResponse响应、WebException响应异常、HttpChannelFactory1工厂)
位于System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求、HttpWebResponse响应、HttpChannelFactory
1工厂、WebException响应异常、ChannelBinding ChannelBinding)
位于System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度超时)
位于System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时)
位于System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)
在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作、布尔单向、ProxyOperationRuntime操作、对象[]输入、对象[]输出、时间跨度超时)
位于System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage方法调用,ProxyOperationRuntime操作)
位于System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)
在[0]处重试异常:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)
at System.Runtime.Remoting.proxy.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)
在MyApplication.FirstDataReference.ServiceSoap.SendAndCommit(SendAndCommitRequest请求)中
位于MyApplication.FirstDataReference.ServiceSoapClient.MyApplication.FirstDataReference.ServiceSoap.SendAndCommit(SendAndCommitRequest请求)
在MyApplication.FirstData.Merchant.Charge(字符串订单ID、字符串持卡人姓名、字符串卡号、字符串金额、字符串到期月份、字符串到期年份、字符串ccv、字符串地址、字符串邮政编码)
在MyApplication.controls.Confirmation.Step2SubmitButton_单击(对象发送者,事件参数e)
我想相信,当我的应用程序尝试使用服务引用而不是端点本身时,会出现问题 我回答这个问题已经很晚了,但这是您的生产环境中TLS设置的症状吗?