- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 WCF 的新手,正在尝试使用自定义用户名和密码创建 WCF 服务。我知道我应该将用户名和密码设置为代理的 ClientCredentials,但出于某种原因,我没有这样的属性...
我认为它与我的契约(Contract)有关,所以这里是:
我的合约代码:
namespace Contracts
{
[ServiceContract]
public interface ICalc
{
[OperationContract]
CalcResponse Add(double x, double y);
[OperationContract]
CalcResponse Substract(double x, double y);
[OperationContract]
CalcResponse Multiply(double x, double y);
[OperationContract]
CalcResponse Divide(double x, double y);
}
}
在我的客户中,我尝试做的是:
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
ICalc proxy = channel.CreateChannel();
proxy.ClientCredentials.UserName.UserName = "USER";
proxy.ClientCredentials.UserName.Password = "PASSWORD";
但是我的代理没有 ClientCredentials 属性
更新:我遇到了一些导致其他错误的问题。当我解决它们时,我遇到了一个新错误:
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
我的超时时间在客户端和服务器端都设置为 5 分钟。我在不到一分钟后收到此错误...
这是我更新后的代码:
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials);
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "Comply";
loginCredentials.UserName.Password = "123456";
channel.Endpoint.Behaviors.Add(loginCredentials);
ICalc proxy = channel.CreateChannel();
我有一个自定义验证器,我在那里设置了一个断点,但它并没有成功...我的验证器代码:
class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == "comply" && password == "123456")
{
}
}
}
配置(在客户端和服务器上):
<service name ="WcfServiceLibrary.Service1" behaviorConfiguration ="CustomValidator">
<host>
<baseAddresses>
<add baseAddress="http://localhost/CalcIISHost/mex"/>
<add baseAddress ="net.tcp://localhost:808/CalcIISHost/CalcService"/>
</baseAddresses>
</host>
<endpoint address ="" binding ="netTcpBinding" bindingConfiguration="tcpWithMessageSecurity" contract ="Contracts.ICalc"></endpoint>
<endpoint address ="net.tcp://localhost:5001/CalcIISHost/mex" binding ="mexTcpBinding" contract ="IMetadataExchange"></endpoint>
</service>
...
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WCFClasses.CustomUserNameValidator, WCFClasses"/>
<serviceCertificate
findValue="localhost"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="My" />
</serviceCredentials>
<serviceMetadata httpGetEnabled ="true"/>
</behavior>
...
<netTcpBinding>
<binding name="tcpWithMessageSecurity" sendTimeout="00:05:00" receiveTimeout="00:05:00">
<security mode="Message" >
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
知道我做错了什么吗?
最佳答案
您可以像这样设置凭据...
删除默认端点行为
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials);
创建凭据
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "USER";
loginCredentials.UserName.Password = "PASSWORD";
将凭据设置为工厂的新端点行为
channel.Endpoint.Behaviors.Add(loginCredentials);
ICalc proxy = channel.CreateChannel();
编辑 2 月 27 日
我建议向您的服务添加日志记录,并在您的 Error.svclog 文件中发布您遇到的异常,在您的服务 app.config 中的配置标签下添加以下内容。
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true" >
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
</listeners>
</source>
<source name="myUserTraceSource"
switchValue="Information, ActivityTracing">
<listeners>
<add name="xml"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="Error.svclog" />
</sharedListeners>
</system.diagnostics>
关于c# - 具有 ClientCredentials 属性的 CreateChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878795/
我正在尝试使用 WCF 服务: 服务的配置是:
我可以从客户端消息检查器中引用代理客户端实例吗? 原因,我想访问以下属性的值: ClientCredentials.UserName.UserName ClientCredentials.UserN
我可以从客户端消息检查器中引用代理客户端实例吗? 原因,我想访问以下属性的值: ClientCredentials.UserName.UserName ClientCredentials.UserN
我在 SSL 下的本地 IIS 上托管了 WCF 服务。它似乎与 SL 客户端一起工作。但我坚持用户身份验证。我有带有用户名和密码文本框的登录屏幕。一旦用户点击登录按钮,肯定会发生一些事情,但是会发生
我是 WCF 的新手,正在尝试使用自定义用户名和密码创建 WCF 服务。我知道我应该将用户名和密码设置为代理的 ClientCredentials,但出于某种原因,我没有这样的属性... 我认为它与我
在我使用相互证书验证客户端的 WCF 自托管 WebService 中,我设置了 CertificateValidationMode = PeerTrust 但它似乎被忽略了,因为我仍然可以使用我已删
我使用客户端凭据创建 WCF 应用程序。它在内部工作正常,而我从其他项目引用和调用此服务意味着我收到了错误消息。 The username is not provided. Specify usern
我正在 WCF 中创建自定义 channel 以实现自定义安全协议(protocol)。不,不要逃!没那么可怕! 验证服务协议(protocol)相对简单。困难的部分是根据客户端凭据将安全信息添加到请
我正在 Java EE 中实现一个针对 Web 服务的代理,并且我已经从该 Web 服务的 WSDL 生成了 stub 。但是,在 Web 服务的用户手册中,所有示例均采用 C# 编写,并且手册指出您
我的任务是从本地 wsdl 文件创建 WCF 服务。我在这个任务上做了一些研发。我添加了该本地 wsdl 文件的 Web 引用。现在我想为此服务创建一个客户端以便访问该服务。 如何在客户端提供用户名和
是否可以在 App.config 中为 WCF 设置客户端凭据? 我想避免这样做: Using svc As New MyServiceClient svc.ClientCredentials.U
我有一个在服务器 A 上运行的 Web 应用程序调用服务器 B 上的 WCF 服务(K2 工作流),该服务需要模拟/委派才能作为调用用户运行。如果我从服务器 A 运行 Web 应用程序,该应用程序运行
为什么我的 Azure AD 应用程序不允许 oauth client_credentials 授予? 我想使用 Azure Graph API,但首先我需要一个 oauth token 。为了获取
我使用 HTTPS 开发了一个带有自定义 UserNamePasswordValidator 和 basicHttpBinding 的 WCF 服务。它与 .Net 客户端配合得很好,使用 Clien
我想使用 Azure 数据工厂将数据从 Azure Blob 传输到 Azure SQL 数据库。我找到了这个链接:Copy data from Azure Blob to Azure SQL Dat
我想使用 Azure 数据工厂将数据从 Azure Blob 传输到 Azure SQL 数据库。我找到了这个链接:Copy data from Azure Blob to Azure SQL Dat
我已经实现了一个测试客户端/服务器,它通过 WCF 实现了 UserName 消息身份验证。它几乎一切正常,但我在最后一个障碍上倒下了。 我得到一个 InvalidOperationException
我不得不放弃基本的 WCF UserName/Pwd 安全性并实现我自己的自定义客户端凭据,以保存默认情况下提供的更多信息。 我通过了 this MSDN article ,但我错过了一些东西,因为它
我有一个 WCF web 服务托管在 IIS 中。这是相同的配置: 当我尝试在我的 WinForms 客户端中使用此特定服务(作为服务引用添加时)时,它会抛出此异常: The username is
我正在尝试在 Azure Functions 上使用新的 Microsoft Graph 绑定(bind)功能,因此我遵循 this article (使用clientCredentials): Az
我是一名优秀的程序员,十分优秀!