【问题标题】:WCF - can't get Identity.UserName inside serviceWCF - 无法在服务中获取 Identity.UserName
【发布时间】:2010-12-01 17:54:59
【问题描述】:

我正在尝试制作一个必须与 WCF 服务交互的 WP7 应用程序。
我想要的是传递用户名/密码并在 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>

我也有自定义用户名验证器,但它从未被调用

    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");
        }
    }
}

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

【问题讨论】:

    标签: wcf authentication wcf-binding wcf-security


    【解决方案1】:

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

    您的自定义验证器代码不会被调用,因为服务配置为 clientCredentialType="None" - 即没有身份验证。

    【讨论】:

    • 还没有测试,但似乎是我们需要的,tnx。
    • 仅供参考 - 链接现已损坏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多