如何在Windows身份验证中传递默认凭据 [英] How to pass default credentials in Windows Authentication
问题描述
我正在使用C#.net开发UWP应用程序,并且它具有启用Windows身份验证的WCF服务。在使用添加服务引用选项使用服务呼叫后,我无法通过默认的NetworkCredential。
I'm developing UWP application using C#.net and it has WCF service with Windows Authentication enabled. I struggling to pass default NetworkCredential after consume a service call using Add service reference option.
请在我的检查下方找到。
Please find below my examinations.
当我通过正确的Windows身份验证凭据时,它会按预期工作。
When I pass correct windows authentication credentials, it is working as expected.
var service = new ServiceReference.Service1Client();
service.ClientCredentials.Windows.ClientCredential =new NetworkCredential("pradeep","****");
var test = await service.GetDataAsync(1);
但是,我想在使用此服务时传递默认的网络凭据
but, I wanted pass default network credentials while using my service 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;
当我通过默认凭据时。我遇到了例外。
When I pass default credentials. I'm getting below exception.
HTTP请求已通过客户端身份验证方案
Negotiate进行了未授权的操作。从服务器收到的身份验证标头是
'Negotiate,NTLM'。
The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate, NTLM'.
我在默认的NetworkCredential中测试了相同的服务调用WPF应用程序按预期方式工作。
I tested same service call with default NetworkCredential in WPF application which is working as expected.
推荐答案
为了通过使用UWP传递UWP中WCF Windows身份验证的默认凭据, System.Net.CredentialCache.DefaultNetworkCredentials ,首先请确保您已添加企业身份验证和专用网络(客户端和服务器)功能如下所示:
In order to pass the default credentials for the WCF Windows Authentication in UWP by using the System.Net.CredentialCache.DefaultNetworkCredentials, first please make sure that you have added the Enterprise Authentication and Private Networks(Client & Server) capabilities as following:
对于企业身份验证功能,这是因为Windows域凭据使用户可以使用其凭据登录到远程资源,并像用户证明标识了他们的用户名和密码。企业身份验证特殊功能通常用于连接到企业内服务器的业务应用程序中。
For the Enterprise Authentication capability, it is because that Windows domain credentials enable a user to log into remote resources using their credentials, and act as if a user provided their user name and password. The enterprise Authentication special capability is typically used in line-of-business apps that connect to servers within an enterprise.
对于专用网络(客户端和服务器) 功能,这是因为当前在Windows运行时中,我们只能在Intranet中传递默认凭据。对于Internet,我们必须使用用户名和密码作为凭据。
For the Private Networks(Client & Server) capability, it is because that currently in Windows Runtime we can only pass the default credential in the Intranet. For the Internet we have to use the Username and Password as credential.
有关功能的更多信息,请检查:
https://msdn.microsoft.com/en-us/library/windows/apps/hh464936。 aspx 。
For more information about the Capabilities, please check: https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx .
之后,请尝试使用您的计算机名称或完全合格的计算机名称 WCF服务的IP地址,如下所示: http:// YourComputerName:YourPortNumber / Service1.svc 。
After that please try to use your Computer name or Fully Qualified Computer name instead of the IP address for your WCF Services like this: http://YourComputerName:YourPortNumber/Service1.svc.
最后,请使用另一台计算机作为客户端,使用 System.Net.CredentialCache.DefaultNetworkCredentials 在UWP中测试WCF Windows身份验证,然后应该工作正常。
At last please use another computer as client to test the WCF Windows Authentication in UWP with the System.Net.CredentialCache.DefaultNetworkCredentials, then it should work fine.
谢谢。
这篇关于如何在Windows身份验证中传递默认凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!