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

  • 相关阅读:
    CSS 之 样式优先级机制
    SQL Server 之 修改时不允许保存更改
    IP_TOS选项
    eXosip2代码分析
    Ubuntu下关闭apache和mysql的开机启动
    linphone3.4.0代码分析
    XMPP/Jingle Vs SIP/SIMPLE 的简单介绍
    git命令常见问题总结
    正向代理与反向代理的区别
    SIP中OPTIONS方法的用法及示例
  • 原文地址:https://www.cnblogs.com/birds-zhu/p/10565604.html
Copyright © 2011-2022 走看看