2011-02-23 45 views
1

您好,传递每个WCF Web服务调用的登录信息?

我已设置了WCF作为web服务(percall),这将web服务获得从大范围的系统请求。现在我们需要一些方法来识别客户。

它可能建立一个CustomUserNamePasswordValidation,但这需要一个安全的(SLL)通信。在我们的情况下,我们不需要安全性,而且解决方案需要尽可能简单地进行设置。

所以问题是如何发送每个电话的客户端身份验证(用户名/密码)?

我可以将识别数据放在header中,但我不知道如何通过示例soupUI来测试这种识别数据?而且我不确定是否所有可以沟通的系统都可以在没有复杂情况下处理这个问题?

任何消耗?

请注意:我只想做1次呼叫,所以不需要使用登录服务方法。

回答

1

WCF就不能支持发送用户凭据不安全。为了解决这个问题,你可以使用the clear username binding或者在邮件头中手动添加证书(这对于WCF来说很简单)

0

定义在象在web.config的结合:

<basicHttpBinding> 
    <binding name="BasicAuthBinding"> 
     <security mode="Message"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 

然后定义一个服务行为,如:

<behavior name="Namespace.TestBehaviour"> 
     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" 
      customUserNamePasswordValidatorType="Namespace.ServiceSecurity.UserAuthenticator, Namespace" /> 
     </serviceCredentials> 
     <serviceAuthorization> 
     <authorizationPolicies> 
      <add policyType="Namespace.ServiceSecurity.MyAuthorizationPolicy, Namespace" /> 
     </authorizationPolicies> 
     </serviceAuthorization> 
    </behavior> 

然后提供自定义的认证和授权类如下:

public class MyAuthorizationPolicy: IAuthorizationPolicy 
{ 
    public bool Evaluate(EvaluationContext evaluationContext, ref object state) 
    { 
     IList<IIdentity> identities = (IList<IIdentity>) evaluationContext.Properties["Identities"]; 

     foreach (IIdentity identity in identities) 
     { 
      if (identity.IsAuthenticated && 
       identity.AuthenticationType == "UserAuthenticator") 
      { 
       evaluationContext.Properties["Principal"] = identity.Name; 
       return true; 
      } 
     } 

     if (!evaluationContext.Properties.ContainsKey("Principal")) 
     { 
      evaluationContext.Properties["Principal"] = ""; 
     } 
     return false; 
    } 

    public ClaimSet Issuer 
    { 
     get { throw new NotImplementedException(); } 
    } 
} 

验证如下:

public class UserAuthenticator : UserNamePasswordValidator 
{ 
    public override void Validate(string userName, string password) 
    { 
     //authenticate me however you want 
     //then set whatever you want 
    } 
} 

如果您需要进一步的安全性,改变basicHttpBinding的到的wsHttpBinding和使用证书

编辑:差点忘了,使用所定义的服务行为,并在web.config中的服务接口定义绑定。

+0

>谢谢!我现在已经实现了这一点,但在浏览服务时得到以下异常:BasicHttp绑定要求BasicHttpBinding.Security.Message.ClientCredentialType与安全消息的BasicHttpMessageCredentialType.Certificate凭证类型相同。为UserName凭证选择Transport或TransportWithMessageCredential安全性。 – Banshee

+0

>我已经尝试设置绑定到这一点:<绑定名称=“BasicAuthIntegration”> <安全模式=“TransportCredentialOnly”> <运输clientCredentialType =“基本”> ,这是将得到的服务会但是当试图从测试客户端调用服务时(使用ClientCredentials.UserName.UserName和ClientCredentials.UserName.Password),我将得到以下异常(在客户端中):HTTP请求未经客户端身份验证方案“Basic”的授权。从服务器收到的验证头是'Basic realm ='localhost''。 – Banshee

0

在代码:

public class WCF_Project_Authentification : UserNamePasswordValidator 
{ 
    #region UserNamePasswordValidator Interface Member 

    public override void Validate(string userName, string password) 
    { 
     if (userName != "Jeanc" || password != "fortin") 
     { 
      throw new FaultException("Authentification failed"); 
     } 
    } 
    #endregion 
} 

在配置:

<behaviors> 
     <serviceBehaviors> 
     <behavior name="Service_Behavior"> 
      <serviceMetadata httpGetEnabled="False" httpsGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="True"/> 
      <serviceCredentials> 

      <userNameAuthentication userNamePasswordValidationMode="Custom" 
            customUserNamePasswordValidatorType="WcfService.Authentification.WCF_Project_Authentification, WcfService"/> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 

并在客户端代码:

proxy.ClientCredentials.UserName.UserName = "Jeanc"; 
proxy.ClientCredentials.UserName.Password = "fortin"; 
+0

@ Jean-Chritophe>谢谢,但我已经知道如何使用UserNamePasswordValidator,问题是我需要传递客户端标识而没有任何安全性。 – Banshee