Sco*_*eed 14 c# asp.net wcf winforms
我有一个我在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>
Run Code Online (Sandbox Code Playgroud)
此服务需要接收WindowsCredentials以根据经过身份验证的用户获取数据库中的信息.此服务当前有一种方法实现具有以下签名的接口
[ServiceContract]
public interface IDesktopService
{
/// <summary>
/// Gets the clients.
/// </summary>
/// <returns>IEnumerable<ClientServiceModel>.</returns>
[OperationContract]
IEnumerable<ClientServiceModel> GetClients();
}
Run Code Online (Sandbox Code Playgroud)
我有一个Windows窗体应用程序正在使用WCF服务,我想使用应用程序传递当前用户的凭据,因为这将全部位于我们的域上.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>
Run Code Online (Sandbox Code Playgroud)
如果我手动设置凭据用户名和密码一切正常,但我一直在尝试
managerService.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials
Run Code Online (Sandbox Code Playgroud)
这样我就可以传递当前用户的凭据,但是在调用GetClients()时,我一直收到错误,即没有设置用户名和密码.
谁能帮我?我也尝试过添加
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
Run Code Online (Sandbox Code Playgroud)
方法,但这没有任何区别.
您可以使用以下方法之一获取用户名(以及密码以外的其他信息):
//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);
Run Code Online (Sandbox Code Playgroud)
但是,您无法访问用户的密码.请参考这个问题
收到身份信息后,您可以使用OutgoingMessageHeaders将用户名传递给WCF,例如:
MessageHeader<string> header = new MessageHeader<string>(userName);
OperationContextScope contextScope = new OperationContextScope(InnerChannel);
OperationContext.Current.OutgoingMessageHeaders.Add(
header.GetUntypedHeader("String", "System"));
Run Code Online (Sandbox Code Playgroud)
在WCF服务实现中,您可以像下面这样阅读:
OperationContext context = OperationContext.Current;
if (context != null)
{
try
{
_LoginName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("String", "System");
}
catch
{
_LoginName = string.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
如果这有帮助或者您有任何其他问题,请告诉我.
谢谢,Soma.
小智 5
我觉得你可以在这里找到你的答案:内联网 - 网络到远程wcf CredentialCache.DefaultNetworkCredentials无法正常工作.基本上,您无法使用Windows窗体应用程序中的Windows身份验证到WCF.您只能直接将其与WCF或表单一起使用.
归档时间: |
|
查看次数: |
702 次 |
最近记录: |