积极答复者
用户登录与验证的问题,很苦恼

问题
-
答案
-
#若需要限制IP,只能在ServiceOperation里实现了。
是的。
当然也可以写MessageInspector,用AOP的形式插入IP过滤功能,这样可能会整洁一些
#在Service的函数里,如何获取到用户通过ClientCredentials.UserName.UserName传过来的用户名
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
全部回复
-
那你也可以使用其它验证方式啊。 比如对客户端使用简单的Basic验证。 Proxy提供一次验证之后,服务端针对这个Proxy的调用都会共享一个验证结果。 调用方法里传用户名密码的问题是,即使验证失败,也必须实例化服务类。而且中间有消息的映射过程。 效率会低很多。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
怎么弄?
我的宿主用的是Windows Service或者命令行应用程序,不是IIS
你是回我的帖子么?如果用的不是iis 估计不能用httpmodule...看看下面这个有用没用。。。
-
ASP.NET 中,用户的credential实际上是保存在服务端的,用户只是拥有一个key,随着cookie存在于整个会话过程中。 若你对ASP.net form authentication 和 Membership熟悉的话,你可以尝试启用 WCF的 AspNetCompatibility 模式, 并且配置binding,使其允许在HTTP上携带cookie。参见文章:
http://msdn.microsoft.com/en-us/library/aa702682.aspx
另外,WCF提供了非常丰富的authentication/authorization方法,至于你想使用自定义的用户名密码方式验证,我建议你选择 TransportWithMessageCredential模式,你需要一个证书用作HTTPS通道,然后配置username authentication方式,并利用membership的基础设施。
关于如何配置TransportWithMessageCredential且使用Username凭证,参考
http://mogliang.blogspot.com/2010/06/how-to-host-wcf-on-cloud-with-transport.html (可能要代理才能访问)
关于如何配置Membership provider,参考
http://msdn.microsoft.com/en-us/library/ms731049.aspx
你先选择验证方式,并自己先实验一下,将遇到的问题帖出来,大家会协助你解决
谢谢,
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
十分感谢版主的帮助
我觉得WCF这东西越做问题越多,而且百度都搜不到解决方法,只能来论坛求救
我有个问题,启用AspNetCompatibility 是不是以为着宿主只能是IIS?
我现在用的就是TransportWithMessageCredential模式
这是我的配置文件:
<bindings> <wsHttpBinding> <binding name="bindingConfiguration"> <security mode="TransportWithMessageCredential"> <transport clientCredentialType="Basic"></transport> <message clientCredentialType="UserName"/> </security> <reliableSession enabled="true"/> </binding> </wsHttpBinding> </bindings>
<serviceCredentials> <serviceCertificate storeName="My" x509FindType="FindBySubjectName" findValue="192.168.1.14" storeLocation="LocalMachine"/> <clientCertificate> <authentication certificateValidationMode="None"/> </clientCertificate> <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="TeachInfoPlatform.UserPasswordValidator,TeachInfoPlatform"/> </serviceCredentials>
虽然已经实现了自定义用户名和密码的功能,但是这不能满足我的需求
我在自定义的身份验证方法中,不仅需要验证用户名和密码,还要验证对方IP地址,由于在身份验证时,服务还没有实例化,因此OperationContract.Context是空的,没法获取IP,请问版主还有没有其他方法获取IP?
验证身份完毕后,我还希望从数据库中提取该用户的权限信息,以后用户调用函数时,可以根据用户的权限信息,判断能否调用该函数。不知道这个能不能实现
-
# 启用AspNetCompatibility 是不是以为着宿主只能是IIS?
AspNetCompatibility 要求WCF是和ASP.net application host在一起,也就是说不能使用WCF selfhost。
# 我在自定义的身份验证方法中,不仅需要验证用户名和密码,还要验证对方IP地址,由于在身份验证时,服务还没有实例化,因此OperationContract.Context是空的,没法获取IP,请问版主还有没有其他方法获取IP?
UserNamePasswordValidator 是运行在独立于Service实例的线程上,无法访问OperationContract.Context。你若需要限制IP,只能在ServiceOperation里实现了。
# 我还希望从数据库中提取该用户的权限信息,以后用户调用函数时,可以根据用户的权限信息,判断能否调用该函数
这实际上是Authorization功能,WCF实现了此功能,参考
http://msdn.microsoft.com/en-us/library/ms733071.aspx
http://msdn.microsoft.com/en-us/magazine/cc948343.aspx
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
# 启用AspNetCompatibility 是不是以为着宿主只能是IIS?
AspNetCompatibility 要求WCF是和ASP.net application host在一起,也就是说不能使用WCF selfhost。
# 我在自定义的身份验证方法中,不仅需要验证用户名和密码,还要验证对方IP地址,由于在身份验证时,服务还没有实例化,因此OperationContract.Context是空的,没法获取IP,请问版主还有没有其他方法获取IP?
UserNamePasswordValidator 是运行在独立于Service实例的线程上,无法访问OperationContract.Context。你若需要限制IP,只能在ServiceOperation里实现了。
# 我还希望从数据库中提取该用户的权限信息,以后用户调用函数时,可以根据用户的权限信息,判断能否调用该函数
这实际上是Authorization功能,WCF实现了此功能,参考
http://msdn.microsoft.com/en-us/library/ms733071.aspx
http://msdn.microsoft.com/en-us/magazine/cc948343.aspx
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
若需要限制IP,只能在ServiceOperation里实现了。ServiceOperation指的就是具体的服务的代码实现过程?
我还有一个问题,在Service的函数里,如何获取到用户通过ClientCredentials.UserName.UserName传过来的用户名
-
#若需要限制IP,只能在ServiceOperation里实现了。
是的。
当然也可以写MessageInspector,用AOP的形式插入IP过滤功能,这样可能会整洁一些
#在Service的函数里,如何获取到用户通过ClientCredentials.UserName.UserName传过来的用户名
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
#若需要限制IP,只能在ServiceOperation里实现了。
是的。
当然也可以写MessageInspector,用AOP的形式插入IP过滤功能,这样可能会整洁一些
#在Service的函数里,如何获取到用户通过ClientCredentials.UserName.UserName传过来的用户名
OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
我们最好先把问题范围缩小。
- 你可以建立一个无安全设置的Endpoint,检查一下是否使用此Endpoint也有性能问题。
- 检查是否网络连接速度很差。
- 你也可以打开 WCF Trace,看webservice调用过程中是否发生了异常
另外我记得WCF3.5有一个已知问题,当返回数组,并且设置了安全时,会有效率问题,你能否告诉我你的OperationContract和Web.config中<system.ServiceModel>节?你用的.net版本是多少?
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
你笔记本有没配置网络代理?
你尝试设置 basichttpbinding 的 UseDefaultWebProxy 为false,看看是否解决问题
你能否测试一下网络是否有问题?比如服务端host一个网页,用客户端访问,速度如何?
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework -
#用的是wshttpbinding,这样客户端向服务端提交信息的时候,是不是信息的大小会受到服务端buffer的限制
消息大小不受maxBufferSize限制,但是受maxReceivedMessageSize限制。
Mog Liang
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework