如何实现基于消息/传输安全验证机制下的windows身份验证过程、无任何验证

本文详细介绍了Windows Communication Foundation (WCF) 中的Windows验证机制。包括如何在消息安全模式(message security mode)和传输安全模式(transport security mode)下启用Windows验证,并展示了具体的代码配置示例。

前言:

《如何实现基于消息/传输安全验证机制下的username身份验证过程》已经介绍了如何通过消息安全模式进行username的身份验证过程,本文将继续介绍WCF支持的宁外一种身份验证机制-windows认证。

message security mode和transport security mode 下都支持windows验证。当然,支持windows验证的前提是WCF service和WCF client都必须在domain里面。这样当client请求时,服务器端才能将客户端身份映射成domain中相对应的windows帐号(kerberos或者NTLM)。

正文:

WCF service type的定义还是采用上篇的定义。为了看出程序的运行效果,我们将serviceType的constructor稍作修改,如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
   
public class MyMath:IMyMath
    {
        
public MyMath()
        {
            System.Security.Principal.WindowsIdentity identity
=System.ServiceModel.ServiceSecurityContext.Current.WindowsIdentity;
            
if (identity != null)
            {
                Console.WriteLine(
"welcome:{0}", identity.Name);
                Console.WriteLine(
"MyMath constructor is called");
            }
        }
        
public int Sum(int x, int y)
        {
            
return x + y;
        }
    }

其作用很简单,当客户端身份能够映射成相应windows帐号时,则在服务器端显示当前用户名。

windows验证模式的config如下:

主要是binding中的security修改为windows,如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
<bindings>
<wsHttpBinding>
<binding name="NewBinding0">
<security>
<message clientCredentialType="Windows" />
</security>
</binding>
</wsHttpBinding>
</bindings>

 

然后创建客户端程序,将wcf service启动之后,运行client,可以看到客户端身份被正确映射成windows域帐号。 

运行时服务器端结果如下:

不进行任何验证(none)的配置:

ContractedBlock.gif ExpandedBlockStart.gif Code
<bindings>
            
<wsHttpBinding>
                
<binding name="NewBinding0">
                    
<security>
                        
<message clientCredentialType="None" negotiateServiceCredential="false" />
                    
</security>
                
</binding>
            
</wsHttpBinding>
        
</bindings>

运行结果如下:

转载于:https://www.cnblogs.com/Winston/archive/2008/10/30/1322983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值