【问题标题】:Asp.Net 4.5 Consume Service Reference with windows authentication and default credentials带有 Windows 身份验证和默认凭据的 Asp.Net 4.5 使用服务参考
【发布时间】:2015-02-15 02:11:17
【问题描述】:

我的 Intranet 上有一个 Web 应用程序配置为在 IIS 上使用 Windows 身份验证。我们有一个 windows 网络和一个活动目录服务器,所以当一些用户尝试访问应用程序时,浏览器不会要求登录和密码,因为它使用当前登录用户的 windows 凭据。

我在此应用程序中添加了一个 Web 服务,并尝试在 Windows 应用程序中使用它。我尝试时收到错误 401。用户已经使用自己的凭据登录 Windows,所以我想让我的 Windows 应用程序使用当前用户的凭据登录到 Web 服务。

我的服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MyWebServices
{
    /// <summary>
    /// Summary description for TestWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TestWS : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

我的控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWebServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {

            TWS.TestWSSoapClient svc = new TWS.TestWSSoapClient();
            try
            {

                Console.WriteLine(svc.HelloWorld());
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
            finally
            {
                Console.ReadLine();
            }

        }
    }
}

都是.net framework 4.5

对不起我的英语不好:)

【问题讨论】:

    标签: c# web-services windows-authentication


    【解决方案1】:

    我找到了。 我需要创建一个基本的 http 绑定并将其客户端凭据类型设置为 Windows。它也适用于 ntlm。

    我必须创建一个端点地址并将 http 绑定和端点地址传递给我的 SoapClient 构造函数。

    static void Main(string[] args)
    {
    
    
        try
        {
            System.ServiceModel.BasicHttpBinding MyHttpBinding = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly);                                            
            MyHttpBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
            //MyHttpBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Ntlm;  //works too
            System.ServiceModel.EndpointAddress MyAuthASMXWebServiceAddress = new System.ServiceModel.EndpointAddress(new Uri("http://localhost/TestWS.asmx"));
            TWS.TestWSSoapClient svc = new TWS.TestWSSoapClient(MyHttpBinding, MyAuthASMXWebServiceAddress);
    
            Console.WriteLine(svc.HelloWorld());
        }
        catch (Exception err)
        {
            Console.WriteLine(err.ToString());
        }
        finally
        {
            Console.ReadLine();
        }
    
    }
    

    我仍然不知道 BasicHttpSecurityMode.TransportCredentialOnly 是什么意思以及其他选项是什么,它们是如何工作的。但是,就目前而言,它奏效了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-10
      • 2010-12-08
      • 2016-07-07
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2018-12-05
      • 2014-03-01
      相关资源
      最近更新 更多