WCF - 无法在服务中获取Identity.UserName

时间:2010-12-01 17:55:00

标签: wcf authentication wcf-binding wcf-security

我正在尝试创建一个必须与WCF服务交互的WP7应用程序 我想要的是传递userName /密码并在WCF服务中看到它。

        Service1Client proxy = new Service1Client();
        proxy.ClientCredentials.UserName.UserName= "abc@email.com";
        proxy.ClientCredentials.UserName.Password = "abc";

我们在服务中尝试获取经过身份验证的名称的代码(但我们获得了Windows身份标识 - MyPc \ MyLogin)

HttpContext.Current.User.Identity.Name

这是客户端配置

<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647">
                <!--<security mode="None" />-->
              <security mode="TransportCredentialOnly">
                <!--<transport clientCredentialType="Basic" />-->                   
              </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:45148/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

这是服务器配置

        <?xml version="1.0"?>
        <configuration>
           <system.web>
            <compilation debug="true" targetFramework="4.0" />
          </system.web>
          <system.serviceModel>
            <behaviors>
              <serviceBehaviors >
                <behavior>
                  <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                  <serviceMetadata httpGetEnabled="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="true"/>
                  <serviceCredentials>
                    <userNameAuthentication userNamePasswordValidationMode="Custom"
                     customUserNamePasswordValidatorType="WP7Service.WpfCustomValidator, WP7Service" />
                  </serviceCredentials>
                </behavior>
              </serviceBehaviors>
            </behaviors>
            <bindings>
              <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1">
                  <security mode="TransportCredentialOnly">
                    <transport clientCredentialType="None" />
                  </security>
                </binding>
              </basicHttpBinding>
            </bindings>
            <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
          </system.serviceModel>
          <system.webServer>
            <modules runAllManagedModulesForAllRequests="true"/>
          </system.webServer>
        </configuration>

我也有cutom用户名验证器,但它永远不会被称为

    public class WpfCustomValidator :
      System.IdentityModel.Selectors.UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == null || password == null)
        {
            throw new ArgumentNullException();
        }
        var rf = WindsorAccessor.InitializeRepositoryFactory(userName);

        if (!ValidateLogOn(rf, userName, password))
        {
            throw new FaultException("Incorrect Username or Password");
        }
    }
}

我希望答案显示如何配置客户端和服务,以便能够通过ClientCredentials.UserName.UserName或通过当前线程标识(或任何其他方式)查看服务中的HttpContext.Current.User.Identity.Name

1 个答案:

答案 0 :(得分:1)

Dominick Baier shows you how do to do this在他的博客上。

您的自定义验证程序代码未被调用,因为该服务已配置为clientCredentialType =“None” - 即无身份验证。