WCF安全,用户名密码无证书

标签 wcf iis-7.5 wcf-binding wcf-security

我是 的新手周转 .我习惯了 *.asmx 但它会被弃用,所以我决定深入研究 WCF。我想为我的服务提供一个简单的用户名 + 密码身份验证,但在网络上到处都是关于 X509 证书的。我想在 中托管我的服务IIS ,所以我将启用 SSL 那里。

我已经关注了一些关于 WCF 的 hello world 教程,但对所有新事物、datacontract、OperationContract、ServiceContract、所需接口(interface)、web.config 中的所有绑定(bind)有点困惑。 , basicHttpBinding 等。

我目前在File -> New project -> Visual C# -> WCF -> WCF Service Application
我有一种 hello world 应用程序,并且想知道保护它的最好和最简单的方法是什么。我读过很多不同的东西,我只是不知道什么是最适合我的情况。

托管在 IIS 中的服务将在 Internet 上可用(启用 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>

和我的解决方案资源管理器:

solution explorer

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

Microsoft Service Configuration Editor

最佳答案

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

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

在 IIS 中设置好后,您需要编辑 WCF 绑定(bind)以启用 SSL。

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

<basicHttpBinding>
    <binding name="SecureBinding" receiveTimeout="01:00:00">
      <security mode="Transport" />
    </binding>
</basicHttpBinding>

和安全行为,以下指定将使用的 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>

然后您需要创建一个安全端点
<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>

这应该使您能够在托管网站后在您的网站上使用 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)

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

希望这可以帮助

编辑

如果你想向服务发送用户名和密码,你只需要在服务中创建一个新方法。

您将在接口(interface)文件 (IService1.cs) 中定义操作协定
[OperationContract]
bool Login(string password,string username);

然后您将在服务类 (Service1.svc) 中创建该方法
public bool Login(string password,string username)
{
    //Your Code to check the username and password here
}

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

您需要创建一个继承自 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>

现在您已经设置了成员(member)提供商,您可以将您的登录代码从上面更改为以下代码,此代码将对用户进行身份验证并设置授权 cookie。
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;
    }
}

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

关于WCF安全,用户名密码无证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6584794/

相关文章:

WCF IIS 托管服务由单个服务实现的多个服务契约(Contract) - 如何通过配置在端点之间共享 uri

c# - 添加对控制台应用程序的 Web 服务引用

c# - 日志记录作为 WCF 中的横切关注点

asp.net-mvc-4 - 附加到 HTTP 响应的 CORS header 未发送到浏览器

https - 解决 AS2 连接问题 - TLS 握手后发送 TCP RST

wcf - 验证消息的安全性时出错

c# - 什么时候选择套接字编程而不是 WCF?

wcf - 为什么 kerberos 在 WCF 中默认为 NTLM?

javascript - IIS 7.5 没有图像 css js 显示

wcf - 在 app.config 中指定默认 WCF 端点