WCF安全性,无证书的用户名密码
我是新来的WCF。我习惯于使用*.asmx,但它会被弃用,所以我决定深入研究WCF。我希望为我的服务提供一个简单的用户名+密码身份验证,但在web上的任何地方都需要X509证书。我想在IIS中托管我的服务,因此我将在那里启用SSL 我已经学习了一些关于WCF的hello world教程,但对所有的新事物,datacontract、OperationContract、ServiceContract、必需的接口、web.config中的所有绑定、basicHttpBinding等都有点困惑 我目前在WCF安全性,无证书的用户名密码,wcf,iis-7.5,wcf-binding,wcf-security,Wcf,Iis 7.5,Wcf Binding,Wcf Security,我是新来的WCF。我习惯于使用*.asmx,但它会被弃用,所以我决定深入研究WCF。我希望为我的服务提供一个简单的用户名+密码身份验证,但在web上的任何地方都需要X509证书。我想在IIS中托管我的服务,因此我将在那里启用SSL 我已经学习了一些关于WCF的hello world教程,但对所有的新事物,datacontract、OperationContract、ServiceContract、必需的接口、web.config中的所有绑定、basicHttpBinding等都有点困惑 我目前在
File->newproject->visualc#->WCF->WCF服务应用程序
我有一种hello world应用程序,我想知道保护它的最好和最简单的方法是什么。我读了太多不同的东西,我只是不知道什么对我的情况是最好的
IIS中托管的服务将在internet上可用(启用ssl),并且我希望将用户名和密码发送给几个受信任的人
请告诉我最简单和合适的安全措施
编辑
我正试图关注这篇博文:
但是我在发布元数据时遇到了麻烦。我假设我的web.config中有错误
<system.serviceModel>
<services>
<service behaviorConfiguration="WcfServiceSimStars.MyServiceTypeBehaviors" name="FarmService.CustomerDeskOperations">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="RequestUserName" contract="WcfServiceSimStars.ISimService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="RequestUserName" >
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://mytestserver/simservice.svc" binding="WSHttpBinding"
bindingConfiguration="WSHttpBinding_ISimService" contract="WcfServiceSimStars.ISimService"
name="WSHttpBinding_ISimService" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceSimStars.MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceSimStars.UserValidatorr, WcfServiceSimStars" />
<serviceCertificate findValue="Farm" storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
和我的解决方案资源管理器:
编辑2:
我试图从visual studio工具菜单中使用Microsoft服务配置编辑器打开我的web.config,但出现以下错误:
如果要使用SSL,则需要使用X509证书
如果要在IIS上托管它并在那里启用SSL,则需要提供证书,出于调试目的,可以从IIS中生成自签名证书
一旦在IIS中进行了设置,您将需要编辑WCF绑定以启用SSL
您将需要具有安全模式传输集的绑定
<basicHttpBinding>
<binding name="SecureBinding" receiveTimeout="01:00:00">
<security mode="Transport" />
</binding>
</basicHttpBinding>
另一种方法是使用加密而不是SSL。在客户端加密密码并将加密数据发送到服务,但我不确定这样做的最佳实践
希望这有帮助
编辑
如果您想向服务发送用户名和密码,您只需要在服务中创建一个新方法
您可以在接口文件(IService1.cs)中定义操作契约
然后在服务类(Service1.svc)中创建方法
这可能是最简单的方法。另一种更复杂的方法是使用自定义成员资格提供程序对用户进行身份验证
您需要创建一个从MembershipProvider继承的类,并重写ValidateUser方法
public class SampleMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
//check the username and password here
}
//No need to override the other methods just leave them
....
}
现在,您需要告诉webconfig使用表单身份验证和自定义成员资格提供程序
<authentication mode="Forms" />
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add name="CustomMembershipProvider" type="SampleApplication.CustomMembershipProvider" />
</providers>
</membership>
现在,当您在服务上调用一个方法时,您可以检查用户是否已通过身份验证,如果已通过身份验证,您可以运行该命令,否则不可以
bool DoWork()
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
//do something
return true;
}
else
{
return false;
}
}
如果您需要我澄清任何事情,请告诉我好的,我应该如何让服务接受用户名+密码?谢谢@midimatt的所有努力!将开始使用以下内容:)如果您使用的是sslI,则您的mexHttpBinding需要是mexHttpBinding。IIS中尚未启用SSL。如果要在不使用SSLokay的情况下进行测试,则在尝试发布元数据时会出现什么错误?它表示此服务的发布元数据是不允许的。您的绑定称为RequestUserName,但在您的端点中,您引用的是wshttpbinding\u ISimService。端点中的绑定配置应与您为绑定指定的名称匹配。
[OperationContract]
bool Login(string password,string username);
public bool Login(string password,string username)
{
//Your Code to check the username and password here
}
public class SampleMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
//check the username and password here
}
//No need to override the other methods just leave them
....
}
<authentication mode="Forms" />
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add name="CustomMembershipProvider" type="SampleApplication.CustomMembershipProvider" />
</providers>
</membership>
public bool Login(string password,string username)
{
if (Membership.ValidateUser(username, password))
{
FormsAuthentication.SetAuthCookie(username, false);
return true;
}
return false;
}
bool DoWork()
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
//do something
return true;
}
else
{
return false;
}
}