Silverlight3系列(二)Silverlight3+wcf+在不使用证书的情况下自定义用户名密码验证
2010-01-25 12:04 Virus-BeautyCode 阅读(3807) 评论(9) 编辑 收藏 举报
先说一下我的需求。
系统需求:
系统是一个电子商务平台,可以提供信息的展示,购买和交易(交易将来考虑)。其实和淘宝是一样的,区别就是淘宝是一个综合类的,什么产品都上的,我们是一个行业性的,垂直的。
技术选型:
Silverlight3
WCF
MS SQL
功能需求:
客户端可以直接通过http访问,不需要使用https,而且也不需要安装证书。我们的wcf服务不想直接暴露在Internet中,但是不要使用https访问,也不要证书验证,因为大部分还是信息的浏览,将来的交易部分肯定是需要https,甚至是需要安装证书的,目前不需要这些。
设计

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;
}
}
}

<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>

client.ClientCredentials.UserName.UserName = "admin";
client.ClientCredentials.UserName.Password = "admin";
_sysUser = new SysUser() { UserName = "swb", Password = "swb" };
LoadCustomers();
GetCustomerById();
client.SayHelloAsync(_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+在不使用证书的情况下自定义用户名密码验证)中我对安全问题,又找了一些新的想法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2008-01-25 场馆和健身馆的区别
2008-01-25 SQL“多字段模糊匹配关键字查询”[转载]