C# 基本身份验证和WCF
我试图学习WCF,但我真的不明白我必须做什么。我有一个带有用户名和密码的数据库,用户应该在使用该服务之前进行身份验证 目前,用户名和密码是硬编码的:C# 基本身份验证和WCF,c#,wcf,security,C#,Wcf,Security,我试图学习WCF,但我真的不明白我必须做什么。我有一个带有用户名和密码的数据库,用户应该在使用该服务之前进行身份验证 目前,用户名和密码是硬编码的: class UsernameAuthentication : UserNamePasswordValidator { /// <summary> /// When overridden in a derived class, validates the specified username and password.
class UsernameAuthentication : UserNamePasswordValidator
{
/// <summary>
/// When overridden in a derived class, validates the specified username and password.
/// </summary>
/// <param name="userName">The username to validate.</param><param name="password">The password to validate.</param>
public override void Validate(string userName, string password)
{
var ok = (userName == "test") && (password == "test");
if (ok == false)
throw new AuthenticationException("username and password does not match");
}
}
我的问题是:要使web.config文件起作用,我到底需要更改什么?我看过一些教程,但并不真正理解所需的更改
另外,我正在尝试做的是——在用户访问服务之前对其进行身份验证,这是正确的方法吗
谢谢
编辑:
我的配置文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Binding1">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.UsernameAuthentication, service1" />
</serviceCredentials>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
错误:无法激活service1.svc。您必须在web.config中指定您将使用用户名/密码凭据,并使用自定义密码验证程序 服务的绑定应该设置一种安全类型(
传输
或消息
,最适合您的安全类型),对于这种安全类型,您必须设置要使用的凭据(用户名和密码)
其中,Microsoft.ServiceModel.Samples.CalculatorService
是自定义验证器所在的命名空间,customusernamevalidater
是自定义验证器(UserNamePasswordValidator
在您的例子中),而service
是服务的名称
否则,服务将需要一个默认的验证器,如ASP.NET成员资格提供程序
服务凭据必须放在您的服务行为中
另外,不要忘记将行为与服务定义联系起来
<services>
<service behaviorConfiguration="ServiceBehavior" name="ServiceName">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="ContractName" />
....
</service>
</services>
....
注意:web.config中还有很多我没有显示的设置。元素的名称只是方向性的。这只是为了使用户名凭据起作用
你可以查看MSDN,因为他们有很多关于这方面的优秀教程,比如这本
是的,事实上,如果您以正确的方式配置它,它将在授予客户端(用户、客户端服务)运行服务方法的权限之前对其进行身份验证。您必须在web.config中指定您将使用用户名/密码凭据,并使用自定义密码验证程序 服务的绑定应该设置一种安全类型(
传输
或消息
,最适合您的安全类型),对于这种安全类型,您必须设置要使用的凭据(用户名和密码)
其中,Microsoft.ServiceModel.Samples.CalculatorService
是自定义验证器所在的命名空间,customusernamevalidater
是自定义验证器(UserNamePasswordValidator
在您的例子中),而service
是服务的名称
否则,服务将需要一个默认的验证器,如ASP.NET成员资格提供程序
服务凭据必须放在您的服务行为中
另外,不要忘记将行为与服务定义联系起来
<services>
<service behaviorConfiguration="ServiceBehavior" name="ServiceName">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="ContractName" />
....
</service>
</services>
....
注意:web.config中还有很多我没有显示的设置。元素的名称只是方向性的。这只是为了使用户名凭据起作用
你可以查看MSDN,因为他们有很多关于这方面的优秀教程,比如这本
是的,事实上,如果您以正确的方式配置它,它将在授予客户机运行服务方法的权限之前对客户机(用户、客户机服务)进行身份验证。在您的服务行为中,我必须将?放在哪里?嘿,我已经添加了配置文件。你能看一下吗?谢谢,通过向行为添加名称,它可以编译,但不使用验证器。您没有定义服务,这意味着在标记下。这应该是自动创建的,您应该只添加行为的名称。您的web.config缺少许多必要的标记。请参阅有关MSDN的更多文档。你的问题太笼统了。在你的服务行为中,我到底应该在
下面的哪个位置放置?嘿,我已经添加了配置文件。你能看一下吗?谢谢,通过向行为添加名称,它可以编译,但不使用验证器。您没有定义服务,这意味着在标记下。这应该是自动创建的,您应该只添加行为的名称。您的web.config缺少许多必要的标记。请参阅有关MSDN的更多文档。你的问题太笼统了。
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" />
</serviceCredentials>
.....
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="ServiceName">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="ContractName" />
....
</service>
</services>