【问题标题】:How to pass ASP.NET authentication to a WCF Service如何将 ASP.NET 身份验证传递给 WCF 服务
【发布时间】:2015-11-05 09:43:42
【问题描述】:

我有一个带有基本身份验证的 WCF 服务,它需要用户名和密码。我在胖客户端中使用此服务,用户名和密码存储在应用程序中,因此可以轻松传递。

我现在想将此服务与 ASP.NET 应用程序一起使用。我启用了安全性,它工作正常。我想知道将这些凭据发送到我的 Web 服务的最佳方式。使用this.User.Identity.Name我可以很容易得到的用户名,但是密码比较难。当然我可以将它存储在一个加密的会话变量中,但这是正确的解决方案吗?下面的代码片段显示了当前的硬编码密码:-

MyServiceClient client = new MyServiceClient();
client.ClientCredentials.UserName.UserName = this.User.Identity.Name;
client.ClientCredentials.UserName.Password = "Password";

顺便说一句:这是我在这里找到答案多年后的第一个问题,所以请放轻松:-)

【问题讨论】:

    标签: asp.net wcf authentication


    【解决方案1】:

    启用身份验证服务 如果您还没有 ASP.NET Web 应用程序,请创建一个。 将服务文件 (.svc) 添加到包含以下指令以引用 AuthenticationService 类的网站,如以下示例所示: VB

    <%@ ServiceHost 
      Language="VB"
      Service="System.Web.ApplicationServices.AuthenticationService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    C#
    <%@ ServiceHost 
      Language="C#"
      Service="System.Web.ApplicationServices.AuthenticationService" 
      Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
    

    在 Web.config 文件中进行以下配置设置以配置服务并要求 SSL: 在 authenticationService 元素中启用身份验证服务。 在 services 元素中定义端点契约,在 behavior 元素中定义服务行为。如以下示例所示,在端点合同中包含 bindingNamespace 属性,以防止某些代理生成工具出现异常。有关 WCF 终结点的详细信息,请参阅 Windows Communication Foundation 终结点。 为 ASP.NET 兼容性配置 serviceHostingEnvironment 元素。有关托管 WCF 服务的详细信息,请参阅 WCF 服务和 ASP.NET。 在需要 SSL 的绑定元素中创建绑定。有关 WCF 中传输安全的详细信息,请参阅传输安全。 以下示例显示了 Web.config 文件中的 system.serviceModel 元素,该文件显示了上一个列表中描述的配置设置。

    <system.web.extensions>
      <scripting>
        <webServices>
          <authenticationService enabled="true" 
             requireSSL = "true"/>
        </webServices>
      </scripting>
    </system.web.extensions>
    <system.serviceModel>
      <services>
        <service name="System.Web.ApplicationServices.AuthenticationService"
            behaviorConfiguration="AuthenticationServiceTypeBehaviors">
          <endpoint contract=
            "System.Web.ApplicationServices.AuthenticationService"
            binding="basicHttpBinding"
            bindingConfiguration="userHttps" 
            bindingNamespace="http://asp.net/ApplicationServices/v200"/>
          </service>
      </services>
      <bindings>
            <basicHttpBinding>
                <binding name="userHttps">
                    <security mode="Transport" />
                </binding>
            </basicHttpBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="AuthenticationServiceTypeBehaviors">
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment 
        aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
    To configure forms authentication
    In the Web.config file, configure the Web application to use forms authentication.
    The following example shows the authentication element in a Web.config file that is configured to use forms authentication.
    <authentication mode="Forms">
      <forms cookieless="UseCookies" />
    </authentication>
    

    身份验证服务需要 cookie。因此,在 authentication 元素中,将 cookieless 属性设置为“UseCookies”。有关详细信息,请参阅 ASP.NET 表单身份验证概述。 安全 如果您要传递敏感的用户数据,例如身份验证凭据,请始终通过安全套接字层(SSL,使用 HTTPS 协议)访问身份验证服务。有关如何设置 SSL 的信息,请参阅配置安全套接字层(IIS 6.0 操作指南)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      相关资源
      最近更新 更多