zoukankan      html  css  js  c++  java
  • WebService的应用之winform身份验证

    大家会了web服务,确实很好用,他可以通过定义soap头来定义自己的用户名密码,如下:

    //webservice中添加类
    //自定义Soap头,用户名密码
    public class SecurityHeader : System.Web.Services.Protocols.SoapHeader
    {
        
    private string _username;
        
    private string _userpass;
        
    public string UserName
        
    {
            
    set
            
    {
                _username 
    = value;
            }

            
    get
            
    {
                
    return _username;
            }

        }

        
    public string UserPass
        
    {
            
    set
            
    {
                _userpass 
    = value;
            }

            
    get
            
    {
                
    return _userpass;
            }

        }
    在service类中定义
    public SecurityHeader header;
    嘎嘎,注意了啊!
    在service中添加处理方法
    //用户名密码比对
        private bool ValidateUser(string user, string pass)
        {
            if (user.Equals("user") && pass.Equals("user"))
                return true;
            else
                return false;
        }
    下面才是要点(其实哪个都很重要)
        [WebMethod, SoapHeader(" header")]
        public string HelloWorld()
        {
            if (ValidateUser(npssa.UserName, npssa.UserPass))//比对之后返回
            {
                return "Hello World";
            }
            else
            {
                return "error";
            }
        }
    服务端配置完了,客户端配置:
    private myService.Service ms;
    private void Init()
            {
                try
                {
                    myService.SecurityHeader header = new Nowind.myService.SecurityHeader();//定义头
                    header.UserName = textBox1.Text;//用户名
                    header.UserPass = textBox2.Text;
                    ms = new Nowind.myService.Service();//定义web引用
                    ms.SecurityHeaderValue = header;//加入头信息
                    textBox3.Text=ms.HelloWorld();

                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
    //也是别人写的自己看了,写出来的
    如何让webservice的命名空间和方法在通过IE查看的时候不生成页面或者隐藏命名空间什么的呢.
  • 相关阅读:
    详解瀑布流布局的5种实现方式及object-fit
    npm私服配置使用
    node快速计算md5
    ajax下载文件实现接受json数据和文件流两种形式
    mac items+sshpass实现服务器登录管理
    web前端私有化部署方案
    chrome devTool中请求各项参数含义
    electron-updater实现electron应用程序更新
    Electron-builder打包应用程序策略
    electron打包配置方案
  • 原文地址:https://www.cnblogs.com/whitetiger/p/814026.html
Copyright © 2011-2022 走看看