gpt4 book ai didi

c# - 获取 DefaultNetworkCredentials 以传递给 WCF 服务

转载 作者:可可西里 更新时间:2023-11-01 07:57:23 26 4
gpt4 key购买 nike

我有一个在 WebApplication 中创建的 WCF 服务,在 web.config 中具有以下配置

<service name="RedwebServerManager.WebApplication.DesktopService"
behaviorConfiguration="ServBehave">
<endpoint
address=""
binding="basicHttpBinding"
bindingConfiguration="basicBind"
contract="RedwebServerManager.WebApplication.IDesktopService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicBind">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>

此服务需要接收 WindowsCredentials 以根据经过身份验证的用户获取数据库中的信息。此服务当前有一个方法实现具有以下签名的接口(interface)

    [ServiceContract]
public interface IDesktopService
{
/// <summary>
/// Gets the clients.
/// </summary>
/// <returns>IEnumerable&lt;ClientServiceModel&gt;.</returns>
[OperationContract]
IEnumerable<ClientServiceModel> GetClients();
}

我有一个使用 WCF 服务的 Windows 窗体应用程序,我想传递使用该应用程序的当前用户的凭据,因为它们都位于我们的域中。 app.config如下

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDesktopService">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows" proxyCredentialType="Windows"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://redwebservermanager.redweb.network/DesktopService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDesktopService"
contract="ManagerService.IDesktopService" name="BasicHttpBinding_IDesktopService" />
</client>
</system.serviceModel>

如果我手动设置凭据用户名和密码一切正常但我一直在尝试

managerService.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials

这样我就可以传递当前用户的凭据,但我在调用 GetClients() 时一直收到错误消息,提示未设置用户名和密码。

谁能帮帮我?我也尝试添加

[OperationBehavior(Impersonation = ImpersonationOption.Required)]

方法,但这没有任何区别。

最佳答案

您可以使用以下方式之一获取用户名(以及密码以外的更多信息):

//1. 
System.Security.Principal.WindowsIdentity.GetCurrent();
//2.
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal user = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
//3.
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal user = new WindowsPrincipal(ident);

但是,您无法访问用户的密码。请引用这个question

收到身份信息后,您可以使用 OutgoingMessageHeaders 将用户名传递给 WCF,例如:

MessageHeader<string> header = new MessageHeader<string>(userName);
OperationContextScope contextScope = new OperationContextScope(InnerChannel);
OperationContext.Current.OutgoingMessageHeaders.Add(
header.GetUntypedHeader("String", "System"));

在 WCF 服务实现中,您可以这样阅读:

OperationContext context = OperationContext.Current;

if (context != null)
{
try
{
_LoginName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("String", "System");
}
catch
{
_LoginName = string.Empty;
}
}

如果这对您有帮助或者您有任何其他问题,请告诉我。

谢谢,体细胞。

关于c# - 获取 DefaultNetworkCredentials 以传递给 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36936592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com