WCF Windows集成身份验证详细步骤。

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

How to: Use basicHttpBinding with Windows Authentication and TransportCredentialOnly in WCF from Windows Forms

            7 out of 29 rated this helpful - Rate this topic                         

patterns & practices Developer Center

Applies To

  • Microsoft Windows Communication Foundation (WCF) 3.5
  • Microsoft Visual Studio 2008

Summary

This how-to article walks you through the process of using Windows authentication over the basicHttpBinding binding using the TransportCredentialsOnly security mode. The article shows you how to configure WCF, configure Internet Information Services (IIS) for Windows authentication, and test the service with a sample WCF client.

Contents

                 

Objectives

  • Learn how to create a WCF service hosted in IIS.
  • Learn how to expose the WCF service as a legacy Web service through basicHttpBinding.
  • Learn how to call the service from a test client.
                 

Overview

Windows authentication is well suited for scenarios in which your users have domain credentials. In the scenario described in this how-to article, users are authenticated by using Windows authentication. The basicHttpBinding binding is used in order to provide support for older clients that expect a legacy ASMX Web service. The TransportCredentialOnly security mode option passes the user credentials without encrypting or signing the messages. Use this mode with caution as it will not protect the credentials being transmitted and they will have to be protected by some other means, such as Internet Protocol Security (IPSec).

In this how-to article, you will create a sample WCF service in Visual Studio 2008. You will then configure the service to use basicHttpBinding with TransportCredentialOnly security through the use of the WCF Configuration Editor. You will enable Windows authentication in IIS to allow your users to authenticate to the service. Finally, you will create a test client to verify that the service is working properly.

                 

Summary of Steps

  • Step 1: Create a Sample WCF Service
  • Step 2: Configure the WCF Service to Use basicHttpBinding
  • Step 3: Configure the basicHttpBinding to use Windows Authentication with TransportCredentialOnly
  • Step 4: Enable Windows Authentication on IIS
  • Step 5: Create a Windows Forms Test Client Application
  • Step 6: Add a WCF Service Reference to the Client
  • Step 7: Test the Client and WCF Service
                 

Step 1: Create a Sample WCF Service

In this step, you create a WCF service in Visual Studio, hosted in an IIS virtual directory. 

  1. In Visual Studio, on the File menu, click New Web Site.
  2. In the Templates section, select WCF Service. Make sure that the Location is set to Http, and specify http://localhost/WCFServiceBasicHttp as the Path. Click OK in the New Web Site dialog box to create a virtual directory and a sample WCF service.
  3. Browse to your WCF service at http://localhost/WCFServiceBasicHttp/Service.svc.

    You should see your WCF service respond with details of the service.

                 

Step 2: Configure the WCF Service to Use basicHttpBinding

In this step, you configure your WCF service endpoint to use basicHttpBinding

  1. Right-click the Web.config file of the WCF service and then click Edit WCF Configuration.

    If you do not see the Edit WCF Configuration option, on the Tools menu, click WCF Service Configuration Editor. Close the WCF Service Configuration Editor tool that appears. The option should now appear on the web.config context menu.

  2. In the Configuration Editor, in the Configuration section, expand Service and then expand Endpoints.
  3. Select the first node [Empty Name]. Set the name attribute to BasicHttpEndpoint.

    By default, the name will be empty because it is an optional attribute.

  4. In the Service Endpoint section, set the binding attribute to basicHttpBinding by choosing this option from the drop-down list.
  5. In the Configuration Editor, on the File menu, click Save.
  6. In Visual Studio, verify your configuration settings in Web.config. The configuration should look as follows:
    …
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="" binding="basicHttpBinding"
            name="BasicHttpEndpoint"
            bindingConfiguration=""
            contract="IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding"
           contract="IMetadataExchange" />
      </service>
    </services> 
    …
    
    
                 

Step 3: Configure basicHttpBinding to use Windows Authentication with TransportCredentialOnly

By default, the basicHttpBinding security mode is None. This default setting means that you do not have authentication and that neither transport nor message security is enabled. By enabling Windows authentication with TransportCredentialOnly, you will get authentication, but no message protection; this is similar to how an ASMX Web service works.

  1. In the Configuration Editor, in the Configuration section, select the Bindings folder.
  2. In the Bindings section, choose New Binding Configuration.
  3. In the Create a New Binding dialog box, select basicHttpBinding.
  4. Click OK.
  5. Set the Name of the binding configuration to some logical and recognizable name; for example, BasicHttpEndpointBinding.
  6. Click the Security tab.
  7. Set the Mode attribute to TransportCredentialOnly by choosing this option from the drop-down menu.
  8. Set the TransportClientCredentialType to Windows by choosing this option from the drop-down list.

    In this case, the Windows option represents Kerberos.

  9. In the Configuration section, select BasicHttpEndpoint.
  10. Set the BindingConfiguration attribute to BasicHttpEndpointBinding by choosing this option from the drop-down list.

    This associates the binding configuration setting with the binding.

  11. In the Configuration Editor, on the File menu, click Save.
  12. In Visual Studio, verify your configuration, which should look as follows:
    ...
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpEndpointBinding"
          name="BasicHttpEndpoint" contract="IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding"
            contract="IMetadataExchange" />
      </service>
    </services>
    ...
    
    
                 

Step 4: Enable Windows Authentication on IIS

In this step, you enable IIS for Windows authentication to match the authentication scheme used in your WCF service.

  1. Open Internet Information Services (IIS) Manager by running the inetmgr command from the command line.
  2. Browse to the WCF Service virtual directory created in Step 1.
  3. Right-click the virtual directory and then click Properties.
  4. In the Properties dialog box, click the Directory Security tab.
  5. In the Authentication and access control section, click Edit.
  6. In the Authentication Methods dialog box, clear the Enable anonymous access check box, and then select the Integrated Windows authentication check box.
  7. In the Authentication Methods dialog box, click OK.
  8. In the Properties dialog box, click Apply and then click OK.
  9. Run the iisreset command from the command line.
  10. Verify that your service is working correctly. In IIS Manager, browse to your service (Service.svc).
Ff648505.note(en-us,PandP.10).gifNote:
Important: Make sure that you have installed ASP.NET on your machine; if not or if in doubt, run the following command:
> c:\Windows\Microsoft.NET\Framework\vX.X.XXXXX\aspnet_regiis.exe /i

                 

Step 5: Create a Windows Forms Test Client Application

In this step, you create a Windows Forms application to test the WCF service.

  1. Right-click your solution, click Add, and then click New Project.
  2. In the Add New Project dialog box, in the Templates section, select Windows Application.
  3. In the Name field, type Test Client and then click OK to create a Windows Forms application.
                 

Step 6: Add a WCF Service Reference to the Client

In this step, you add a Web reference of the WCF service to your Client application. This How To article uses a Web reference to show the usage of a WCF service as a legacy Web service; otherwise, you can add it as a service reference.

  1. Right-click your Client project and then click Add Service References.
  2. Click Advanced and then click Add Web Reference under the Compatibility section.
  3. In the Add Web References dialog box, set the URL to your WCF service: http://localhost/WCFServiceBasicHttp/Service.svc
  4. Click Go.
  5. In the Web reference name: field, change localhost to WCFTestService.
  6. Click Add Reference.

    A Web reference to WCFTestService should now appear in your Client project.

                 

Step 7: Test the Client and WCF Service

In this step, you access the WCF service as a legacy ASMX Web service and make sure that it works.

  1. In your Client project, drag a button control onto your form.
  2. Double-click the button control to show the underlying code.
  3. In the code behind the button click, create an instance of the proxy, pass the default user credentials, and call MyOperation1 of your WCF Service. The code should look as follows:
    private void button1_Click(object sender, EventArgs e)
    {
          WCFTestService.Service myService = new 
                                  WCFTestService.Service();
          myService.Credentials = 
                           System.Net.CredentialCache.DefaultCredentials;
          MessageBox.Show(myService.GetData(123, true));
          myService.Dispose();
    }
    
    
  4. Right-click the Client project and then click Set as Startup Project.
  5. Run the Client application by pressing F5 or CTRL+F5. When you click the button on the form, the message “You entered: 123” should appear.

    How to: Use basicHttpBinding with Windows Authentication and TransportCredentialOnly in WCF from Windows Forms

                7 out of 29 rated this helpful - Rate this topic                         

    patterns & practices Developer Center

    Applies To

    • Microsoft Windows Communication Foundation (WCF) 3.5
    • Microsoft Visual Studio 2008

    Summary

    This how-to article walks you through the process of using Windows authentication over the basicHttpBinding binding using the TransportCredentialsOnly security mode. The article shows you how to configure WCF, configure Internet Information Services (IIS) for Windows authentication, and test the service with a sample WCF client.

    Contents

                     

    Objectives

    • Learn how to create a WCF service hosted in IIS.
    • Learn how to expose the WCF service as a legacy Web service through basicHttpBinding.
    • Learn how to call the service from a test client.
                     

    Overview

    Windows authentication is well suited for scenarios in which your users have domain credentials. In the scenario described in this how-to article, users are authenticated by using Windows authentication. The basicHttpBinding binding is used in order to provide support for older clients that expect a legacy ASMX Web service. The TransportCredentialOnly security mode option passes the user credentials without encrypting or signing the messages. Use this mode with caution as it will not protect the credentials being transmitted and they will have to be protected by some other means, such as Internet Protocol Security (IPSec).

    In this how-to article, you will create a sample WCF service in Visual Studio 2008. You will then configure the service to use basicHttpBinding with TransportCredentialOnly security through the use of the WCF Configuration Editor. You will enable Windows authentication in IIS to allow your users to authenticate to the service. Finally, you will create a test client to verify that the service is working properly.

                     

    Summary of Steps

    • Step 1: Create a Sample WCF Service
    • Step 2: Configure the WCF Service to Use basicHttpBinding
    • Step 3: Configure the basicHttpBinding to use Windows Authentication with TransportCredentialOnly
    • Step 4: Enable Windows Authentication on IIS
    • Step 5: Create a Windows Forms Test Client Application
    • Step 6: Add a WCF Service Reference to the Client
    • Step 7: Test the Client and WCF Service
                     

    Step 1: Create a Sample WCF Service

    In this step, you create a WCF service in Visual Studio, hosted in an IIS virtual directory. 

    1. In Visual Studio, on the File menu, click New Web Site.
    2. In the Templates section, select WCF Service. Make sure that the Location is set to Http, and specify http://localhost/WCFServiceBasicHttp as the Path. Click OK in the New Web Site dialog box to create a virtual directory and a sample WCF service.
    3. Browse to your WCF service at http://localhost/WCFServiceBasicHttp/Service.svc.

      You should see your WCF service respond with details of the service.

                     

    Step 2: Configure the WCF Service to Use basicHttpBinding

    In this step, you configure your WCF service endpoint to use basicHttpBinding

    1. Right-click the Web.config file of the WCF service and then click Edit WCF Configuration.

      If you do not see the Edit WCF Configuration option, on the Tools menu, click WCF Service Configuration Editor. Close the WCF Service Configuration Editor tool that appears. The option should now appear on the web.config context menu.

    2. In the Configuration Editor, in the Configuration section, expand Service and then expand Endpoints.
    3. Select the first node [Empty Name]. Set the name attribute to BasicHttpEndpoint.

      By default, the name will be empty because it is an optional attribute.

    4. In the Service Endpoint section, set the binding attribute to basicHttpBinding by choosing this option from the drop-down list.
    5. In the Configuration Editor, on the File menu, click Save.
    6. In Visual Studio, verify your configuration settings in Web.config. The configuration should look as follows:
      …
      <services>
        <service behaviorConfiguration="ServiceBehavior" name="Service">
          <endpoint address="" binding="basicHttpBinding"
              name="BasicHttpEndpoint"
              bindingConfiguration=""
              contract="IService">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
          <endpoint address="mex" binding="mexHttpBinding"
             contract="IMetadataExchange" />
        </service>
      </services> 
      …
      
      
                     

    Step 3: Configure basicHttpBinding to use Windows Authentication with TransportCredentialOnly

    By default, the basicHttpBinding security mode is None. This default setting means that you do not have authentication and that neither transport nor message security is enabled. By enabling Windows authentication with TransportCredentialOnly, you will get authentication, but no message protection; this is similar to how an ASMX Web service works.

    1. In the Configuration Editor, in the Configuration section, select the Bindings folder.
    2. In the Bindings section, choose New Binding Configuration.
    3. In the Create a New Binding dialog box, select basicHttpBinding.
    4. Click OK.
    5. Set the Name of the binding configuration to some logical and recognizable name; for example, BasicHttpEndpointBinding.
    6. Click the Security tab.
    7. Set the Mode attribute to TransportCredentialOnly by choosing this option from the drop-down menu.
    8. Set the TransportClientCredentialType to Windows by choosing this option from the drop-down list.

      In this case, the Windows option represents Kerberos.

    9. In the Configuration section, select BasicHttpEndpoint.
    10. Set the BindingConfiguration attribute to BasicHttpEndpointBinding by choosing this option from the drop-down list.

      This associates the binding configuration setting with the binding.

    11. In the Configuration Editor, on the File menu, click Save.
    12. In Visual Studio, verify your configuration, which should look as follows:
      ...
      <bindings>
        <basicHttpBinding>
          <binding name="BasicHttpEndpointBinding">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Windows" />
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
      <services>
        <service behaviorConfiguration="ServiceBehavior" name="Service">
          <endpoint address="" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpEndpointBinding"
            name="BasicHttpEndpoint" contract="IService">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
          <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
        </service>
      </services>
      ...
      
      
                     

    Step 4: Enable Windows Authentication on IIS

    In this step, you enable IIS for Windows authentication to match the authentication scheme used in your WCF service.

    1. Open Internet Information Services (IIS) Manager by running the inetmgr command from the command line.
    2. Browse to the WCF Service virtual directory created in Step 1.
    3. Right-click the virtual directory and then click Properties.
    4. In the Properties dialog box, click the Directory Security tab.
    5. In the Authentication and access control section, click Edit.
    6. In the Authentication Methods dialog box, clear the Enable anonymous access check box, and then select the Integrated Windows authentication check box.
    7. In the Authentication Methods dialog box, click OK.
    8. In the Properties dialog box, click Apply and then click OK.
    9. Run the iisreset command from the command line.
    10. Verify that your service is working correctly. In IIS Manager, browse to your service (Service.svc).
    Ff648505.note(en-us,PandP.10).gifNote:
    Important: Make sure that you have installed ASP.NET on your machine; if not or if in doubt, run the following command:
    > c:\Windows\Microsoft.NET\Framework\vX.X.XXXXX\aspnet_regiis.exe /i

                     

    Step 5: Create a Windows Forms Test Client Application

    In this step, you create a Windows Forms application to test the WCF service.

    1. Right-click your solution, click Add, and then click New Project.
    2. In the Add New Project dialog box, in the Templates section, select Windows Application.
    3. In the Name field, type Test Client and then click OK to create a Windows Forms application.
                     

    Step 6: Add a WCF Service Reference to the Client

    In this step, you add a Web reference of the WCF service to your Client application. This How To article uses a Web reference to show the usage of a WCF service as a legacy Web service; otherwise, you can add it as a service reference.

    1. Right-click your Client project and then click Add Service References.
    2. Click Advanced and then click Add Web Reference under the Compatibility section.
    3. In the Add Web References dialog box, set the URL to your WCF service: http://localhost/WCFServiceBasicHttp/Service.svc
    4. Click Go.
    5. In the Web reference name: field, change localhost to WCFTestService.
    6. Click Add Reference.

      A Web reference to WCFTestService should now appear in your Client project.

                     

    Step 7: Test the Client and WCF Service

    In this step, you access the WCF service as a legacy ASMX Web service and make sure that it works.

    1. In your Client project, drag a button control onto your form.
    2. Double-click the button control to show the underlying code.
    3. In the code behind the button click, create an instance of the proxy, pass the default user credentials, and call MyOperation1 of your WCF Service. The code should look as follows:
      private void button1_Click(object sender, EventArgs e)
      {
            WCFTestService.Service myService = new 
                                    WCFTestService.Service();
            myService.Credentials = 
                             System.Net.CredentialCache.DefaultCredentials;
            MessageBox.Show(myService.GetData(123, true));
            myService.Dispose();
      }
      
      
    4. Right-click the Client project and then click Set as Startup Project.
    5. Run the Client application by pressing F5 or CTRL+F5. When you click the button on the form, the message “You entered: 123” should appear.
WCF 安全性 四种身份验证方式 WCF 四种身份验证方式 都通过本人测试 qq:574311505 None Windows 自定义证书 自定义用户名密码 立即下载

相关推荐

NodeJS写的WCF代理程序

WCF代理程序,解决端口映射后无法用公网IP访问问题

使用Apache http 访问部署在开启了Windows身份认证(Windows authentication)的IIS下的django工程

前言 开门见山地说,我们都承认在IIS下开启windows authentication是一个非常安全的选项,看看我们的IIS文档是怎么说的: Windows authentication (formerly named NTLM, and also referred to as Windows NT Challenge/Response authentication) is a secure f...

weixin_40153024的博客 572

通过WCF BasicHttpBinding八步实现windows认证

通过WCF BasicHttpBinding八步实现windows认证,想要了解WCF安全的朋友可以下载

WCF BasicHttpBinding 安全解析(4)windows验证(IIS宿主)

      现在我们讨论TransportCredentialOnly安全模式下的安全配置,首先在配置文件中添加如代码清单11-84所示的配置节,配置windows验证Windows凭据认证是基于Windows组账户或者域账户进行认证的方式。在这种认证方式下,客户端进程运行的Window帐号对应的Windows凭证被自动作为调用服务的客户端凭证,所以无需显示指定具体的Windiws凭证。如果...

weixin_34162629的博客 209

Windows Authentication How-To

Tomcat Home The Apache Software Foundation Apache Tomcat 9 Version 9.0.34, Apr 3 2020 Links Docs Home FAQ User Comments User Guide 1) Introduction 2) Setup 3) First webapp 4) Deployer 5) Manager 6) H...

[Blog][Domain] programb.blog.csdn.net 513

WCF读书笔记--安全:基础知识(身份验证、授权、传输安全)

WCF也有一段时间了,我买的是<WCF编程>第二版,这本书还是不错的,知识点很全面,不过就是具体的实例很少,这样对于很多的知识点难免会产生"当时看的时候是理解了,但是过一段时间就会忘记"的现象,而且很多的知识会混淆.写这个系列主要是为了复习一下以前学过的知识,把知识做一个系统的整理,便于记忆,再就是对书中的一些内容写出具体的实例,这样记忆会更深刻,我是初学者,肯定会有理...

weixin_30417487的博客 292

WCF分布式安全开发实践(4):传输安全模式之Windows身份验证:Transport_Windows_NetTcpBinding

Posted on 2009-08-18 23:52 Frank Xu Lei 阅读(1631) 评论(8)  编辑 收藏 网摘 所属分类: WCF分布式安全开发实践, SOA and EAI <!--<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"xmlns:dc="htt

微软特邀讲师 老徐FrankXuLei 的专栏 1881

WCF与IIS集成Windows身份验证的矛盾

好久没有上来了,最近跟着原来的老大一起跳到了一家新公司,在做一个新的交友项目,今天总算基本完成了。分享一个关于WCF的小技巧,由于项目中 很多地方用了Jquery+WCF来实现Ajax异步获取数据,在开发环境下: 直接在vs.net里,右击svc文件在浏览器里浏览时(没有采用vs.net自带的aspx服务器,而是在项目属性里设置为直接使用IIS),提示以下错误:IIS 指定了身份验证方案“In...

weixin_33747129的博客 140

WCF----取消集成windows身份验证 IIS仍无权查看网页

在做一个最简单的WCF DEMO时,发布到IIS后右键点击svc文件浏览,出现“无权查看网页”的页面提示。 查看IIS ,匿名访问已勾选,集成windows身份验证已取消勾选 最后发现是 Internet来宾账户没有启用。 计算机管理--》本地用户和组--》找到IUSER开头的那个  Internet来宾账户,启用。 重新浏览正常了。

奋斗鱼 1066

如何在WCF解决方案中使用WCS(windows cardspace)作为身份验证方式

本文提供了一个引导步骤,讲解了如何在WCF解决方案中采用WCS作为身份验证方式。本文假设你已经清楚地了解了WCF的各项机制。   第一部分:服务部分 1. 服务契约using System.ServiceModel; namespace HelloService { [ServiceContract] public interface IHello { [OperationContract] string Say(); }

陈希章@中国 967

[WCF Security] 2. 安全参数设置

1. 安全方式通过设置 Binding 的属性 Security 来实现。NetTcpBinding binding = new NetTcpBinding();binding.Security.Mode = SecurityMode.Transport;binding.Security.Transport.ProtectionLevel = System.Net.Security.Prote

微软新技术 1177

WCF Security 无进展

正文: WCF Security已经弄了2天了,按着例子测试好像都可以通过,但是按实际的生产环境测试还是有很多问题. 这是这几天参好的一些园子里面的文章,感觉比较好,贴一下. 1. 基本概念 2. 安全参数设置 3. X509 身份验证 4. 用户名/密码身份验证 X.509 & RSA x.509证书在WCF中的应用(CS篇) x.509证书在WCF中的应用(Web/IIS篇) WCF

200

模式与实践 系列发布WCF Security Guidance 1.0

模式与实践 2008年8月1日在 CodePlex 网站上发布了 WCF Security Guidance 中的 Application Scenarios,这是一个运用应用实例来解释WCF身份认证的实际场景,在真实应用程序中面对复杂的 WCF Configuration File 面对有不知如何下手的困境,一个混和 Web Application,Web Services与数据库的身

shanyou的专栏 627

关于WEB Service&WCF&WebApi实现身份验证WCF篇(2)

因前段时间工作变动(换了新工作)及工作较忙暂时中断了该系列文章,今天难得有点空闲时间,就继续总结WCF身份验证的其它方法。前面总结了三种方法(详见:关于WEB Service&WCF&WebApi实现身份验证WCF篇(1)),今天又将分享三种方法,完成WCF篇。 第四种:SOAP Header验证 首先定义一个WCF服务契约及服务实现类...

weixin_33860553的博客 97

[转贴][WCF Security] 2. 安全参数设置

[原文]http://www.rainsts.net/article.asp?id=473 1. 安全方式 通过设置 Binding 的属性 Security 来实现。 NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.Transport; binding.Secur...

weixin_34376562的博客 179
上一篇: ASP.NET--TreeView中被选中Node改变背景或者字体颜色。
Alfred72
博客等级 码龄14年 0粉丝 2原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值