WCF安全性,没有证书的用户名密码

JP *_*ons 6 wcf wcf-binding wcf-security iis-7.5

我是WCF的新手.我习惯了*.asmx,但它会被弃用,所以我决定深入研究WCF.我想为我的服务进行简单的用户名+密码验证,但在网络上的任何地方都是关于X509证书的.我想在IIS中托管我的服务,所以我将在那里启用SSL.

我在WCF上关注了一些hello world教程,但是我对所有新东西,datacontract,OperationContract,ServiceContract,所需的接口,所有绑定web.config,basicHttpBinding等有点困惑.

我现在在 File -> New project -> Visual C# -> WCF -> WCF Service Application

我有一种你好世界的应用程序,并想知道什么是最好和最简单的方法来保护它.我已经阅读了很多不同的东西,我只是不知道什么是最适合我的情况.

IIS中托管的服务将在互联网上提供(启用ssl)以及我想发送给几个可信赖的人的用户名和密码.

请告诉我最简单和最合适的安全性.

编辑 我正在尝试关注此博文:http: //codebetter.com/petervanooijen/2010/03/22/a-simple-wcf-service-with-username-password-authentication-the-things-they-don-t -tell-you / 但是我在发布元数据方面遇到了麻烦.我认为我的错误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>
Run Code Online (Sandbox Code Playgroud)

和我的解决方案探索者:

解决方案探索者

编辑2: 我试图用Microsoft Service Configuration Editorvisual studio工具菜单打开我的web.config 并得到这个错误:

Microsoft服务配置编辑器

Mid*_*att 15

如果要使用SSL,则需要使用X509证书.

如果要在IIS上托管并启用SSL,则需要提供证书,出于调试目的,您可以从IIS中生成自签名证书.

在IIS中进行设置后,您需要编辑WCF绑定以启用SSL.

您将需要与安全模式传输集的绑定

<basicHttpBinding>
    <binding name="SecureBinding" receiveTimeout="01:00:00">
      <security mode="Transport" />
    </binding>
</basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

和安全行为,以下指定将使用的ssl证书.

<behaviors>
  <serviceBehaviors>
    <behavior name="SecureBehavior">
      <serviceMetadata />
      <serviceCredentials>
        <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
      </serviceCredentials>
    </behavior>
    <behavior name="StandardBehavior">
    </behavior>
  </serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

然后,您需要创建一个安全的端点

<services>
  <service behaviorConfiguration="SecureBehavior" name="secureService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecureBinding" contract="<Your webservice class name including namespace>" />
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
  </service>
Run Code Online (Sandbox Code Playgroud)

这应该使您能够在托管后在您的网站上使用SSL.

要从您的客户端连接,您将使用以下(假设C#)

BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
EndPointAddress endpointAddress = new EndpointAddress("Your Service address (including HTTPS)");
Client svc = new Client(binding,endpointAddress)
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用加密而不是SSL.加密客户端密码并将加密数据发送到服务,但我不确定这样做的最佳实践.

希望这可以帮助

编辑

如果要向服务发送用户名和密码,只需在服务中创建新方法即可.

您将在接口文件中定义操作合同(IService1.cs)

[OperationContract]
bool Login(string password,string username);
Run Code Online (Sandbox Code Playgroud)

那么你将在服务类(Service1.svc)中创建方法

public bool Login(string password,string username)
{
    //Your Code to check the username and password here
}
Run Code Online (Sandbox Code Playgroud)

这可能是最简单的方法.另一种更复杂的方法是使用自定义成员资格提供程序来验证用户.

您需要创建一个继承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
    ....
}
Run Code Online (Sandbox Code Playgroud)

现在您需要告诉webconfig使用表单身份验证并使用您的自定义成员资格提供程序

<authentication mode="Forms" />
<membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear />
    <add name="CustomMembershipProvider" type="SampleApplication.CustomMembershipProvider" />
  </providers>
</membership>
Run Code Online (Sandbox Code Playgroud)

现在您已设置了成员资格提供程序,您可以将登录代码从上面更改为以下代码,此代码将对用户进行身份验证并设置授权cookie.

public bool Login(string password,string username)
{
    if (Membership.ValidateUser(username, password))
    {
        FormsAuthentication.SetAuthCookie(username, false);
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

现在,当您在服务上调用方法时,您可以检查用户是否已通过身份验证,如果是,则可以运行该命令,否则不会.

bool DoWork()
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
         //do something
         return true;
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要我澄清任何事情,请告诉我