自定义客户端消费LS的ODATA示例二 解决重复验证



http://blog.csdn.net/greystar/article/details/47107451

上文讲了如何自定义客户消费ODATA,但是不是很完善,如会多一次请求,同时每次请求会到数据库验证一次用户。花了二天时间,在网上找答案,未果。后结启发,觉得还是Cookie本身的问题。因此先登录,后续设置COOKIE。主要难度就是在于lightswitch的登录 ContentType = "application/msbin1"  ,为解决这个格式问题,花了一天时间。

下面为基本代码处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using WpfApplication1.ServiceReference1;
using System.Data.Services.Client;
using System.IO;
using System.Security;
using System.Threading;
using System.Web.ClientServices;
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //application/msbin1 编码库,引用下面
        //https://github.com/GDSSecurity/WCF-Binary-SOAP-Plug-In/blob/master/burp_wcf_plugin/src/NBFS.cs
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }
       //OmsProxy.ApplicationData dataSvc = null;
        ApplicationData dataSvc = null;
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
    
             dataSvc = new ApplicationData(
                    new Uri("http://172.1.1.1/Palau/ApplicationData.svc/"));
            dataSvc.Format.UseJson();
           dataSvc.SendingRequest2 += dataSvc_SendingRequest2;
       
        }

        void dataSvc_SendingRequest2(object sender, SendingRequest2EventArgs e)
        {
            //发生验证COOKIE
                e.RequestMessage.SetHeader("Cookie", _cookie);
            
        }

        string _cookie; 
    string GetCookie(string userName, string password) 
    { 
        if (_cookie == null) 
        {
            string loginUri = "http://172.1.1.1/palau/desktopclient/Web/Microsoft-LightSwitch-Security-ServerGenerated-Implementation-AuthenticationService.svc/binary/Login";
 
            WebRequest request = HttpWebRequest.Create(loginUri); 
            request.Method = "POST";
            request.Headers.Add("Accept-Language", "zh-CN");
            request.Headers.Add("Accept-Encoding", "identity");
            request.ContentType = "application/msbin1";
           
            NBFSNet NBFS = new NBFSNet();
            //登录协议格式
            string userstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Login xmlns=\"http://tempuri.org/\"><userName>" + userName + "</userName><password>" + password + "</password><isPersistent>false</isPersistent><customData i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"></customData></Login>";
            //进行编码处理
           byte[] authBody =  NBFS.EncodeBinaryXML(userstr);
           request.ContentLength = authBody.Length;
            var sss = request.GetRequestStream();
            sss.Write(authBody, 0, authBody.Length);
            sss.Close();

            WebResponse res = request.GetResponse(); 
            if (res.Headers["Set-Cookie"] != null) 
            { 
                _cookie = res.Headers["Set-Cookie"]; 
            } 
            else 
            { 
                throw new SecurityException("Invalid username and password"); 
            } 
        }
        _cookie = _cookie.Replace(",", ";");//这里要替换下符合,服务器设置了二次COOKIE,不然后面不正常
        return _cookie; 
    } 

        private void Button_Click(object sender, RoutedEventArgs e)
        {
        
           string kk = GetCookie("username", "pwd");//先获取登录COOKIE
            var query = from a in dataSvc.ProjectDocuments select a; //再处理时可以直接调用
            MessageBox.Show(query.ToList().Count.ToString());

           var  query1 = from  b in dataSvc.ProjectItemSet select b;
           MessageBox.Show(query1.ToList().Count.ToString());
        }

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值