问题描述
我正在寻找一种方法来为我的 WCF 服务设置简单的用户名 + 密码身份验证。它们是使用 Autofac extensionless services 设置的。这意味着我的 Global.asax 看起来像这样:
public class Global : HttpApplication
{
protected void Application_Start(object sender,EventArgs e)
{
var builder = new ContainerBuilder();
builder.RegisterType<MyWcfService>().InstancePerLifetimeScope();
var container = builder.Build();
AutofacHostFactory.Container = container;
AutofacHostFactory.HostConfigurationAction =
host =>
{
host.Description.Behaviors.Add(container.Resolve<MyCustomBehavior>());
};
RouteTable.Routes.Add(new ServiceRoute("my/custom/route",new AutofacServiceHostFactory(),typeof(MyWcfService)));
}
}
我的 Web.config 看起来像这样:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"></serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="10485760">
<readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760" maxBytesPerRead="10485760" />
</binding>
</basicHttpBinding>
</bindings>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directorybrowse enabled="true" />
</system.webServer>
如您所见,我没有任何 *.svc 文件,我的 Web.config 中也没有 <services>
部分来配置我的服务。现在我需要向这个服务添加基本的用户名 + 密码认证。我已按照this Microsoft tutorial的步骤,但CustomUserNameValidator
从未被调用。
我按照教程所做的更改是:
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="My.ServiceHost.CustomUserNamePasswordValidator,My.ServiceHost"/>
</serviceCredentials>
- 将我在 Web.config 中的绑定更新为:
<wsHttpBinding>
<binding name="authQuotaBinding" maxReceivedMessageSize="10485760">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
<readerQuotas maxDepth="32" maxStringContentLength="10485760" maxArrayLength="10485760" maxBytesPerRead="10485760" />
</binding>
</wsHttpBinding>
在本地,我使用 IIS Express 托管服务。我缺少哪一块拼图才能让它发挥作用?
解决方法
有几条信息缺失,可能是拼图的缺失部分。
-
您通过 Autofac 在所有主机上定义了自定义行为,但不清楚该自定义行为在做什么和/或这是否与您的问题有关?
-
您已经定义了自定义验证器,以便在用户名用于身份验证并需要验证时使用(在 serviceCredentials 部分),但是这只会在您使用 wsHttpBinding 调用服务时触发。但是,我看不到您的服务与此绑定之间的任何链接?您确定该服务正在使用该绑定吗?还是仍在使用您的 basicHttpBinding?通常这会在每个服务的配置中进行配置,但由于您使用的是 AutoFac,我假设 Autofac 将绑定的配置链接到您的服务。可能这就是问题所在?
我的猜测是您需要在 Autofac 中指定它,或者您可能还需要更新这部分配置:
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
进入
<protocolMapping>
<add binding="wsHttpBinding" scheme="http" />
</protocolMapping>
或者如果你想更具体
<protocolMapping>
<add binding="wsHttpBinding" bindingConfiguration="authQuotaBinding" scheme="http" />
</protocolMapping>
但这取决于名为 basicHttpsBinding 的绑定的定义是什么。 (它似乎也没有包含在提供的信息中?)
有关协议映射的更多信息,请参阅:https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/protocolmapping
除此之外,您的要求也可能是使用此配置:
<wsHttpBinding>
...
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
...
</wsHttpBinding>
这当然取决于您是要使用 SOAP 消息加密/签名来保证消息的完整性,还是对 HTTPS 提供的完整性感到满意。
根据您问题中链接的文章,您甚至可以使用 basicHttpBinding 而不是 wsHttpBinding 并使用安全模式设置为 Transport 并且传输部分的 clientCredentialType 属性设置为 Basic 例如,
<basicHttpBinding>
...
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
...
</basicHttpBinding>
有关不同安全模式的详细信息,请参阅:https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/security-of-wshttpbinding
最后,我也不建议保留 serviceDebug 设置,该设置允许您包含在生产环境中设置为 on 的异常详细信息。 (当然,这又取决于您的要求)