c# - 如何在 Windows 身份验证中传递默认凭据

标签 c# win-universal-app windows-10-universal

我正在使用 C#.net 开发 UWP 应用程序,它具有启用了 Windows 身份验证的 WCF 服务。在使用添加服务引用选项使用服务调用后,我努力传递默认 NetworkCredential。

请在下面找到我的检查。

当我通过正确的 Windows 身份验证凭据时,它按预期工作。

var service = new ServiceReference.Service1Client();
service.ClientCredentials.Windows.ClientCredential =new NetworkCredential("pradeep","****");
var test = await service.GetDataAsync(1);

但是,我想在使用我的服务 methis 时传递默认网络凭据

var service = new ServiceReference.Service1Client();
service.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
var test = await service.GetDataAsync(1);

我也尝试了以下选项。

service.ClientCredentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;

当我通过默认凭据时。我低于异常。

The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate, NTLM'.

我在 WPF 应用程序中使用默认 NetworkCredential 测试了相同的服务调用,它按预期工作。

最佳答案

为了使用 System.Net.CredentialCache.DefaultNetworkCredentials 在 UWP 中传递 WCF Windows 身份验证的默认凭据,首先请确保您已添加 Enterprise Authentication专用网络(客户端和服务器) 功能如下:

enter image description here

对于 Enterprise Authentication 功能,这是因为 Windows 域凭据使用户能够使用他们的凭据登录到远程资源,并且就像用户提供了他们的用户名和密码一样。企业身份验证特殊功能通常用于连接到企业内服务器的业务线应用程序。

对于Private Networks(Client & Server)能力,是因为目前在Windows Runtime中我们只能通过Intranet中的默认凭证。对于 Internet,我们必须使用用户名和密码作为凭据。

有关功能的更多信息,请查看: https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx .

之后,请尝试使用您的计算机名完全合格的计算机名 而不是您的 WCF 服务的 IP 地址,如下所示:http://YourComputerName:YourPortNumber/Service1.svc .

最后请使用另一台计算机作为客户端,使用 System.Net.CredentialCache.DefaultNetworkCredentials 测试 UWP 中的 WCF Windows 身份验证,然后它应该可以正常工作。

谢谢。

关于c# - 如何在 Windows 身份验证中传递默认凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126305/

相关文章:

c# - 我的 IEquatable 仍在为 Dictionary<T>[] 使用 Object.GetHashcode

xaml - 我可以在图像名称中使用多个限定符吗? (规模 + 设备系列)

uwp - .net native 编译器 2.0.2 无法构建应用程序。无法加载程序集 System.Data.dll

c# - 为什么我的 UWP 应用无法调用 Type.GetProperties 方法?

c# - 是否可以使用 Windows 应用程序打包项目打包两个 UWP 应用程序?

c# - 在 Asp.net 中使用 HTML5 的拖放上传文件

c# - GIT - 不能忽略 .suo 文件

c# - 在应用程序设置中显示 UWP 应用程序的版本信息

c# - UWP 应用 : Process Memory Usage vs Heap Usage

c# - 我的 C# 程序导致其他计算机出现 APPCRASH