zoukankan      html  css  js  c++  java
  • WCF加密传输数据,b并且用户名+密码验证

    在前2个文章的基础上,继续增加对client 端增加username+password的验证

    1. host增加类Validator,需要添加引用

    using System.IdentityModel.Selectors;

    class Validator : UserNamePasswordValidator

        {

            public override void Validate(string userName, string password)

            {

                if (userName != "admin" || password != "123456")

                {

                    throw new System.IdentityModel.Tokens.SecurityTokenException("Unknown Username or Password");

                }

            }

    }

    1. host中修改binding

    BindingsàwsHttpBindingàbinding下增加

    <security mode="Message">

                <message clientCredentialType="UserName" />

              </security>

    1. host修改Behavior

    BehaviorsàserviceBehaviorsà对应的behavioràserviceCredentials下增加

      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFHost.Validator,WCFHost"/>

    WCFHost.CalcValidator:验证器的类

    WCFHost:验证器的命名空间,dll名字?

    1. Client修改binding

    <wsHttpBinding>

                    <binding name="WSHttpBinding_ICalculator">

                        <security mode="Message">

                            <message clientCredentialType="UserName" />

                        </security>

                    </binding>

                </wsHttpBinding>

    1. 客户端代码

    client.ClientCredentials.UserName.UserName = "admin";

    client.ClientCredentials.UserName.Password = "123456";

    或者

       ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("xxxx");

     factory.Credentials.UserName.UserName="admin1";
     factory.Credentials.UserName.Password = "123456";

    1. 测试

    尝试修改UserName,password,调用不成功

    注意:

    1. 要用username这种方式验证,好像必须使用证书来加密username和password
    2. 用nettcpbinding同样可以证书+username,password来实现,不想试了,
    3.  var name = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;服务端可以查看登录人信息

    源码下载:https://download.csdn.net/download/hanghangz/11042084

  • 相关阅读:
    mysql TO_DAYS()函数
    MySQL year函数
    protobuff java 包编译(Windows)
    苹果笔记本只有电源键能用的解决办法
    linux普通用户获取管理员权限
    linux用户管理
    基于ASIHTTPRequest封装的HttpClient
    Object-C 多线程中锁的使用-NSLock
    appstore 上传需要的icon
    iPhone之IOS5内存管理(ARC技术概述)
  • 原文地址:https://www.cnblogs.com/birds-zhu/p/10565604.html
Copyright © 2011-2022 走看看