Silverlight3系列(二)Silverlight3+wcf+在不使用证书的情况下自定义用户名密码验证...

 

  先说一下我的需求。

  系统需求:

  系统是一个电子商务平台,可以提供信息的展示,购买和交易(交易将来考虑)。其实和淘宝是一样的,区别就是淘宝是一个综合类的,什么产品都上的,我们是一个行业性的,垂直的。

  技术选型:

  Silverlight3

  WCF

  MS SQL

  功能需求:

  客户端可以直接通过http访问,不需要使用https,而且也不需要安装证书。我们的wcf服务不想直接暴露在Internet中,但是不要使用https访问,也不要证书验证,因为大部分还是信息的浏览,将来的交易部分肯定是需要https,甚至是需要安装证书的,目前不需要这些。

 

  设计

  

ExpandedBlockStart.gif MyValidator类代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.IdentityModel.Tokens;
using  System.IdentityModel.Selectors;

namespace  WcfService
{
    
public   class  MyValidator:UserNamePasswordValidator 
    {
        
private   string  _userName;

        
public   string  UserName
        {
            
get  {  return  _userName; }
        }
        
private   string  _password;

        
public   string  Password
        {
            
get  {  return  _password; }
        }
        
public   override   void  Validate( string  userName,  string  password)
        {
            
this ._userName  =  userName;
            
this ._password  =  password;
        }
    }
}

 

 

 

 

 

ExpandedBlockStart.gif wcf的web.config配置
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> < system.serviceModel >
    
        
< services >
      
            
< service  behaviorConfiguration ="WcfService.Service1Behavior"  name ="WcfService.ServiceCustomer" >
        
< host  >
          
< baseAddresses  >
            
< add  baseAddress ="http://sl.kimbanxcn:82/ServiceCustomer.svc" />
          
</ baseAddresses >
        
</ host >
                        
<!-- <endpoint address="" binding="basicHttpBinding" contract="WcfService.IServiceCustomer"> -->
  
< endpoint  address =""  binding ="basicHttpBinding"  bindingConfiguration ="test"  contract ="WcfService.IServiceCustomer" >
                    
< identity >
                        
< dns  value ="sl.kimbanxcn" />
                    
</ identity >
                
</ endpoint >
        
< endpoint  address ="mex"  binding ="mexHttpBinding"  contract ="IMetadataExchange" />
      
</ service >
    
        
</ services >
        
< behaviors >
            
< serviceBehaviors >
                
< behavior  name ="WcfService.Service1Behavior" >
          
< serviceMetadata  httpGetEnabled ="true" />
          
< serviceDebug  includeExceptionDetailInFaults ="false" />
          
< serviceCredentials  >
            
< clientCertificate >
              
< authentication  certificateValidationMode ="None" />
            
</ clientCertificate >
            
<!-- <issuedTokenAuthentication allowUntrustedRsaIssuers="true"></issuedTokenAuthentication> -->        
            
< userNameAuthentication  userNamePasswordValidationMode ="Custom"  customUserNamePasswordValidatorType ="WcfService.MyValidator, WcfService" />
          
</ serviceCredentials >
            
        
</ behavior >
        
            
</ serviceBehaviors >
        
</ behaviors >
    
< bindings  >
      
< basicHttpBinding  >
        
< binding  name ="test" >         
          
< security  mode ="TransportCredentialOnly" >
            
< message  clientCredentialType ="UserName" />
          
</ security >
        
</ binding >
      
</ basicHttpBinding >
    
</ bindings >
    
</ system.serviceModel >

 

 

 

ExpandedBlockStart.gif 客户端的调用代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> client  =   new  ServiceCustomerClient();
            client.ClientCredentials.UserName.UserName 
=   " admin " ;
            client.ClientCredentials.UserName.Password 
=   " admin " ;
            _sysUser 
=   new  SysUser() { UserName  =   " swb " , Password  =   " swb "  };
            LoadCustomers();
            GetCustomerById();
            client.SayHelloAsync(_sysUser);

 

 

ExpandedBlockStart.gif wcf暴露的接口
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->  public string SayHello(SysUser sysUser)
        {
            _myValidator = (MyValidator)OperationContext.Current.Host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator;

            return string.Format("hello,{0},your password is {1}\n{2}{3}", sysUser.UserName, sysUser.Password,
                _myValidator.UserName,_myValidator.Password );
               // ,_myValidator.UserName ,_myValidator.Password );
        }

  可就是不成功,在wcf暴露的方法中设置断点, 跟踪到_myValidator .UserName为null,在MyValidator中设置断点,方法

  public override void Validate(string userName, string password)
        {
            
this._userName = userName;
            
this._password = password;
        }

就没有运行,挂不得是null呢,为什么呢?是不是配置问题呢?我尝试了很多组合,就是出不来效果,是不能这样做呢?还是旧不支持这种需求呢?请各位有空的帮我看看,也请搞过的朋友指点一二,谢谢了!!

   下图是我的WCF的IIS中的身份验证配置,是不是和这里也有什么关系呢?

  msn:jorden008@hotmail.com

  源代码:/Files/virusswb/Silverlight.rar

   

  在下一篇Silverlight+WCF的安全考虑1(紧接上文:Silverlight3+wcf+在不使用证书的情况下自定义用户名密码验证)中我对安全问题,又找了一些新的想法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值