【发布时间】:2021-07-03 07:11:37
【问题描述】:
我正在寻找一种方法来为我的 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。
我按照教程所做的更改是:
- 添加
CustomUserNamePasswordValidator - 将 serviceCredentials 部分添加到 Web.config 中的行为
<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 托管服务。为了让它发挥作用,我缺少哪一块拼图?
【问题讨论】:
标签: c# .net wcf authentication autofac