NetworkCredential UseDefaultCredentials 不起作用

2023-11-22

我们很久以前就编写了一段代码来使用域凭据调用服务。现在域已更改并计划使用默认 Windows 帐户

所以我试图通过配置重置 asmx web 服务的网络凭据。 我创建了一个 SoapExtentsion 类并尝试覆盖那里,即使它执行该行。但它永远不会被应用,并且总是以下现有代码获胜

实际代码

EmployeeService prxy = new EmployeeService();
prxy.Credentials = new System.Net.NetworkCredential("MyAccount", "pwd", "MyDomain");
 //prxy.UseDefaultCredentials = true;// It works but we dont want code change
prxy.GetEmployee(empId);

仅新代码/配置更改

计划通过额外的 dll 和配置更改进行注入。因此,现在不需要/禁止对上述原始代码进行代码更改

public class WindowsDefCredSoapExtn : SoapExtension
{
    public override object GetInitializer(Type serviceType)
    {
        return "";
    }

    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return "";
    }

    public override void Initialize(object initializer)
    {
        //throw new NotImplementedException();
    }

    public override void ProcessMessage(SoapMessage message)
    {

      if (message is SoapClientMessage)
          {
          if (message.Stage == SoapMessageStage.BeforeSerialize)
              {                             
              ((SoapClientMessage)message).Client.Credentials=null;
              (((SoapClientMessage)message)).Client.UseDefaultCredentials = true;

              }
          }                

        }

    }
}


 <system.web>
    <webServices>
      <soapExtensionTypes>
        <add type="MyAssembly.WindowsDefCredSoapExtn, MyAssembly" priority="1" group="Low"/>
      </soapExtensionTypes>
    </webServices>
  </system.web>

有人知道如何通过配置动态更改用于 ASMX 服务的网络凭据吗?

UPDATE:

在调试时,我可以看到网络凭据已重置。但不知何故它在调用请求时发送到服务器。

EmployeeService prxy = new EmployeeService();
prxy.Credentials = new System.Net.NetworkCredential("MyAccount", "pwd", "MyDomain");
 //while debug, prxy.UseDefaultCredentials= false here
//My soap extension gets executed and resets credentials.
prxy.GetEmployee(empId);
 //while debug, prxy.UseDefaultCredentials= true here
 //while debug prxy.Credentials are null here. 

输出窗口:

请参阅最后一行 AcquireCredentialsHandle 通行证authdata。设置的凭据prxy.Credentials = new System.Net.NetworkCredential("MyAccount", "pwd", "MyDomain")尽管我的服务扩展重置了,但仍在使用。

System.Net Verbose: 0 : [11584] WebRequest::Create(http://employee-service/empService.asmx)
System.Net Verbose: 0 : [11584] HttpWebRequest#39256744::HttpWebRequest(http://employee-service/empService.asmx#173042156)
System.Net Information: 0 : [11584] RAS supported: True
System.Net Verbose: 0 : [11584] Exiting HttpWebRequest#39256744::HttpWebRequest() 
System.Net Verbose: 0 : [11584] Exiting WebRequest::Create()    -> HttpWebRequest#39256744
System.Net Verbose: 0 : [11584] HttpWebRequest#39256744::GetRequestStream()
System.Net Information: 0 : [11584] Associating HttpWebRequest#39256744 with ServicePoint#19085264
System.Net Information: 0 : [11584] Associating Connection#65246235 with HttpWebRequest#39256744
System.Net.Sockets Verbose: 0 : [11584] Socket#27993665::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [11584] Exiting Socket#27993665::Socket() 
System.Net.Sockets Verbose: 0 : [11584] Socket#27993665::Connect(45:80#763949146)
System.Net.Sockets Information: 0 : [11584] Socket#27993665 - Created connection from 10.234.80.99:48954 to 10.242.137.45:80
System.Net Information: 0 : [11584] HttpWebRequest#39256744 - Request: POST /empService.asmx HTTP/1.1

System.Net.Sockets Verbose: 0 : [11584] Socket#27993665::Send()
System.Net.Sockets Verbose: 0 : [11584] Data from Socket#27993665::Send
System.Net.Sockets Verbose: 0 : [11584] <<POST /empService.asmx HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.2034)

System.Net.Sockets Verbose: 0 : [11584] Exiting Socket#27993665::Send()     -> 512#512
System.Net Information: 0 : [11584] ConnectStream#50996063 - Sending headers
{
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.2034)
VsDebuggerCausalityData: uIDPo3GOBVodvqZLgUWyCSNWI5kAAAAAzHBQpKvNJ0SvKGwy+a3tGerbsJdijWdHqs2PJipnfqYACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://employee-service/GetProperties"
Host: employee-service
Content-Length: 507
Expect: 100-continue
Connection: Keep-Alive
}.
System.Net.Sockets Verbose: 0 : [11584] Socket#27993665::Receive()
System.Net.Sockets Verbose: 0 : [11584] Data from Socket#27993665::Receive
System.Net.Sockets Verbose: 0 : [11584] <<HTTP/1.1 401 Unauthorized
Content-Length: 0
WWW-Authenticate: NTLM
Date: Fri, 08 Jul 2016 16:11:15 GMT

>>
System.Net.Sockets Verbose: 0 : [11584] Exiting Socket#27993665::Receive()  -> 109#109
System.Net Information: 0 : [11584] Connection#65246235 - Received status line: Version=1.1, StatusCode=401, StatusDescription=Unauthorized.
System.Net Information: 0 : [11584] Connection#65246235 - Received headers
{
Content-Length: 0
Date: Fri, 08 Jul 2016 16:11:15 GMT
WWW-Authenticate: NTLM
}.
System.Net Information: 0 : [11584] ConnectStream#7794715::ConnectStream(Buffered 0 bytes.)
System.Net.Sockets Verbose: 0 : [11584] Socket#27993665::MultipleSend()
System.Net.Sockets Verbose: 0 : [11584] Exiting Socket#27993665::MultipleSend() 
System.Net Verbose: 0 : [11584] Data from ConnectStream#50996063::ResubmitWrite

System.Net Information: 0 : [11584] Associating HttpWebRequest#39256744 with ConnectStream#7794715
System.Net Information: 0 : [11584] Associating HttpWebRequest#39256744 with HttpWebResponse#29471296
System.Net Information: 0 : [11584] Enumerating security packages:
System.Net Information: 0 : [11584]     Negotiate
System.Net Information: 0 : [11584]     Kerberos
System.Net Information: 0 : [11584]     NTLM
System.Net Information: 0 : [11584]     Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [11584]     Schannel
System.Net Information: 0 : [11584]     WDigest
System.Net Information: 0 : [11584]     DPA
System.Net Information: 0 : [11584]     Digest
System.Net Information: 0 : [11584]     MSN
System.Net Information: 0 : [11584] AcquireCredentialsHandle(package  = NTLM, intent   = Outbound, authdata = MyDomain\MyAccount)

UPDATE 2

我刚刚更改了代码进行测试,下面是套接字通信的差异

AcquireDefaultCredential(package = NTLM, intent  = Outbound) 

Vs

AcquireCredentialsHandle(package  = NTLM, intent   = Outbound, authdata = MyDomain\MyAccount)

如果我理解正确,那么我认为您需要在配置文件的 appSettings 下添加帐户、用户名和密码的密钥。

<appSettings>
    <add key="YourDomain" value="yourAccount"/>
    <add key="UserName" value="yourAccount"/>
    <add key="Password" value="yourAccount"/>
</appSettings>

之后在您的代码中,

EmployeeService prxy = new EmployeeService();
prxy.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(), System.Configuration.ConfigurationManager.AppSettings["YourDomain"].ToString());
 //while debug, prxy.UseDefaultCredentials= false here
//My soap extension gets executed and resets credentials.
prxy.GetEmployee(empId);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NetworkCredential UseDefaultCredentials 不起作用 的相关文章

  • 如何在 Cassandra 中存储无符号整数?

    我通过 Datastax 驱动程序在 Cassandra 中存储一些数据 并且需要存储无符号 16 位和 32 位整数 对于无符号 16 位整数 我可以轻松地将它们存储为有符号 32 位整数 并根据需要进行转换 然而 对于无符号 64 位整
  • std::vector 与 std::stack

    有什么区别std vector and std stack 显然 向量可以删除集合中的项目 尽管比列表慢得多 而堆栈被构建为仅后进先出的集合 然而 堆栈对于最终物品操作是否更快 它是链表还是动态重新分配的数组 我找不到关于堆栈的太多信息 但
  • 传递给函数时多维数组的指针类型是什么? [复制]

    这个问题在这里已经有答案了 我在大学课堂上学习了 C 语言和指针 除了多维数组和指针之间的相似性之外 我认为我已经很好地掌握了这个概念 我认为由于所有数组 甚至多维 都存储在连续内存中 因此您可以安全地将其转换为int 假设给定的数组是in
  • 如何在 C++ 中标记字符串?

    Java有一个方便的分割方法 String str The quick brown fox String results str split 在 C 中是否有一种简单的方法可以做到这一点 The 增强分词器 http www boost o
  • 如何使从 C# 调用的 C(P/invoke)代码“线程安全”

    我有一些简单的 C 代码 它使用单个全局变量 显然这不是线程安全的 所以当我使用 P invoke 从 C 中的多个线程调用它时 事情就搞砸了 如何为每个线程单独导入此函数 或使其线程安全 我尝试声明变量 declspec thread 但
  • 访问外部窗口句柄

    我当前正在处理的程序有问题 这是由于 vista Windows 7 中增强的安全性引起的 特别是 UIPI 它阻止完整性级别较低的窗口与较高完整性级别的窗口 对话 就我而言 我想告诉具有高完整性级别的窗口进入我们的应用程序 它在 XP 或
  • 人脸 API DetectAsync 错误

    我想创建一个简单的程序来使用 Microsoft Azure Face API 和 Visual Studio 2015 检测人脸 遵循 https social technet microsoft com wiki contents ar
  • 使用 C# 中的 CsvHelper 将不同文化的 csv 解析为十进制

    C 中 CsvHelper 解析小数的问题 我创建了一个从 byte 而不是文件获取 csv 文件的类 并且它工作正常 public static List
  • 两个静态变量同名(两个不同的文件),并在任何其他文件中 extern 其中一个

    在一个文件中将变量声明为 static 并在另一个文件中进行 extern 声明 我认为这会在链接时出现错误 因为 extern 变量不会在任何对象中看到 因为在其他文件中声明的变量带有限定符 static 但不知何故 链接器 瑞萨 没有显
  • WcfSvcHost 的跨域异常

    对于另一个跨域问题 我深表歉意 我一整天都在与这个问题作斗争 现在已经到了沸腾的地步 我有一个 Silverlight 应用程序项目 SLApp1 一个用于托管 Silverlight SLApp1 Web 的 Web 项目和 WCF 项目
  • 为什么 C# 2.0 之后没有 ISO 或 ECMA 标准化?

    我已经开始学习 C 并正在寻找标准规范 但发现大于 2 0 的 C 版本并未由 ISO 或 ECMA 标准化 或者是我从 Wikipedia 收集到的 这有什么原因吗 因为编写 审查 验证 发布 处理反馈 修订 重新发布等复杂的规范文档需要
  • 两个类可以使用 C++ 互相查看吗?

    所以我有一个 A 类 我想在其中调用一些 B 类函数 所以我包括 b h 但是 在 B 类中 我想调用 A 类函数 如果我包含 a h 它最终会陷入无限循环 对吗 我能做什么呢 仅将成员函数声明放在头文件 h 中 并将成员函数定义放在实现文
  • 复制目录下所有文件

    如何将一个目录中的所有内容复制到另一个目录而不循环遍历每个文件 你不能 两者都不Directory http msdn microsoft com en us library system io directory aspx nor Dir
  • C 函数 time() 如何处理秒的小数部分?

    The time 函数将返回自 1970 年以来的秒数 我想知道它如何对返回的秒数进行舍入 例如 对于100 4s 它会返回100还是101 有明确的定义吗 ISO C标准没有说太多 它只说time 回报 该实现对当前日历时间的最佳近似 结
  • 在 WPF 中使用 ReactiveUI 提供长时间运行命令反馈的正确方法

    我有一个 C WPF NET 4 5 应用程序 用户将用它来打开某些文件 然后 应用程序将经历很多动作 读取文件 通过许多插件和解析器传递它 这些文件可能相当大 gt 100MB 因此这可能需要一段时间 我想让用户了解 UI 中发生的情况
  • C++ 继承的内存布局

    如果我有两个类 一个类继承另一个类 并且子类仅包含函数 那么这两个类的内存布局是否相同 e g class Base int a b c class Derived public Base only functions 我读过编译器无法对数
  • 使用特定参数从 SQL 数据库填充组合框

    我在使用参数从 sql server 获取特定值时遇到问题 任何人都可以解释一下为什么它在 winfom 上工作但在 wpf 上不起作用以及我如何修复它 我的代码 private void UpdateItems COMBOBOX1 Ite
  • 当文件流没有新数据时如何防止fgets阻塞

    我有一个popen 执行的函数tail f sometextfile 只要文件流中有数据显然我就可以通过fgets 现在 如果没有新数据来自尾部 fgets 挂起 我试过ferror and feof 无济于事 我怎样才能确定fgets 当
  • 在OpenGL中,我可以在坐标(5, 5)处精确地绘制一个像素吗?

    我所说的 5 5 正是指第五行第五列 我发现使用屏幕坐标来绘制东西非常困难 OpenGL 中的所有坐标都是相对的 通常范围从 1 0 到 1 0 为什么阻止程序员使用屏幕坐标 窗口坐标如此严重 最简单的方法可能是通过以下方式设置投影以匹配渲
  • 使用 WGL 创建现代 OpenGL 上下文?

    我正在尝试使用 Windows 函数创建 OpenGL 上下文 现代版本 基本上代码就是 创建窗口类 注册班级 创建一个窗口 choose PIXELFORMATDESCRIPTOR并设置它 创建旧版 OpenGL 上下文 使上下文成为当前

随机推荐